Skip to main content

developerWorks >  Ajax  >  Forums  >  Ajax forum  >  developerWorks

Retrieve XML attribute values with AJAX    Point your RSS reader here for a feed of the latest messages in this thread


     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
Permlink Replies: 4 - Pages: 1 - Last Post: Nov 11, 2009 10:27 PM Last Post By: tsuji Threads: [ Previous | Next ]
johnjmk

Posts: 1
Registered: Nov 10, 2009 06:32:08 AM
Retrieve XML attribute values with AJAX
Posted: Nov 10, 2009 06:40:19 AM
Click to report abuse...   Click to reply to this thread Reply
I need to retrieve the attributes from the "trans" elements and print them out into a table from the following XML:

<mydata>
<logon>Trans</logon>
<trans date=\"01-02-2008\" incexp=\"Expense\" item=\"Food\" amount=\"100\">1001</trans>
<trans date=\"01-22-2008\" incexp=\"Expense\" item=\"Shoes\" amount=\"123\">1002</trans>
<trans date=\"01-31-2008\" incexp=\"Expense\" item=\"Rent\" amount=\"1500\">1003</trans>
</mydata>

This is a variation on a previous assignment, so there are portions of code commented out that have nothing to do with this one. My problem is that when I try to assign a variable to the attributes of the trans element I get a value of NULL. If you can get me past this I can get the rest.

When I change the Select Box it's supposed to call the XML. The code can be tested at: http://ntowl.com/testfiles/Kennedy_John_…
(the username/password combinations are user1/pass1, user2/pass2, user3/pass3 to get to the select box).

I didn't know if there were line numbers on this forum, so I put a large comment near the part I'm having trouble with.

Here is the code:

<html>

<script>
/*****************************************
File: asgn6_kennedy.htm
Author: John Kennedy
Assignment: 6
Create Date:
Last Modified:
****************************************/
</script>

<head>
<title>AJAX Finances Logon</title>

<style>
div.AcctInfo
{
position: absolute;
left: 100px;
top: 300px;
height: 400px;
width: 1024px;
font-family: Arial, Helvetica, sans-serif;
}
</style>

<script language = "JavaScript">

var XMLHttpRequestObject = false;

if (window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();
if(XMLHttpRequestObject.overrideMimeType)
{
XMLHttpRequestObject.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject)
{
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}


function getXML(dataSource, divID, data)
{
if(XMLHttpRequestObject)
{
var obj = document.getElementById(divID);
obj.innerHTML = "Loading...";

XMLHttpRequestObject.open("POST", dataSource);
XMLHttpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');


XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200)
{
var myXML = XMLHttpRequestObject.responseXML;
alert(myXML);
obj.innerHTML = "";
displayXML(myXML);
}
}

XMLHttpRequestObject.send("data=" + data);
}

return false;
}

function displayXML(myXML)
{
// alert(myXML);
var XMLLogonElements = myXML.getElementsByTagName("logon");
var myLogon = XMLLogonElements[0].firstChild.data;

var XMLCategoryElements = myXML.getElementsByTagName("category");

// var XMLTransElements = myXML.getElementsByTagName("trans");

var targetDiv = document.getElementById("targetDiv");

if (myLogon == "Success")
{

var logonForm = document.getElementById("logon_form");
logonForm.style.visibilty = "hidden";

var resultString = "<h1>Finances</h1> \
Select Category: \
<select id='myaccts' onchange=getAcctInfo('http://ntowl.com/testfiles/AJAXPHP/displayTrans_detailsXML.php')> \
<option value='-'>-</option>";

for(i = 0; i < XMLCategoryElements.length; i++)
{
resultString += "<option value= " + XMLCategoryElements.firstChild.data + "> \
" + XMLCategoryElements.firstChild.data + "</option>";
}

resultString += "</select>"

logonForm.innerHTML = resultString;

}

else if(myLogon == "Error")
{
targetDiv.innerHTML = "<font color=red>Error: You have input an incorrect username or password</font>";
}

/***********************************************
************************************************

THE PROBLEM IS WITHIN THIS BELOW CONDITIONAL STATEMENT: ATTRIUBTES IS "NULL"

***********************************************
***********************************************/

else if(myLogon == "Trans")
{

var myDataNode = myXML.documentElement;

var logonNode = myDataNode.firstChild;
var transNode = logonNode.firstChild;

// alert(myDataNode);
// alert(logonNode);
// alert(transNode.attributes);

attributes = transNode.attributes;

// alert(attributes)

myDate = attributes.getNamedItem("date");
getIncExp = attributes.getNamedItem("incexp");
getItem = attributes.getNamedItem("item");
getAmount = attributes.getNamedItem("amount");

var myTable = "\n<hr><table border=1>\n";
myTable += "<tr><th>Item</th> \
<th>Inc/Exp</th> \
<th>Item</th> \
<th>Amount</th></tr>";

/*
for(rows = 0; rows < XMLTransElements.length; rows++)
{
myTable += "<tr>" + XMLTransElementsrows.firstChild.data + "</tr>";

}

myTable += "</table>";

objAcctInfo = document.getElementById("AcctInfo");
objAcctInfo.innerHTML = myTable;
*/
}
}

function validateLogon(dataSource, divID)
{
var userName = document.getElementById("userid").value;
var passWord = document.getElementById("password").value;

var data = userName + "|" + passWord;

getXML(dataSource, divID, data);

return false;
}


function getAcctInfo(dataSource)
{
var data = document.getElementById("myaccts").value;
var divID = "AcctInfo";

getXML(dataSource, divID, data);
}

</script>


<body bgcolor="#c0c0c0">


Logon


<form name="form">

<table>
<tr>
<td>Userid:
<td><input type="text" name="userid" id="userid">
</tr>

<tr>
<td>Password:
<td><input type="password" name="password" id="password">
</tr>

<tr>
<td>
<input type=submit value="Logon"
onclick="return validateLogon('http://ntowl.com/testfiles/AJAXPHP/validate_logonxml_accounts.php', 'targetDiv')">
</tr>

</table>
</form>


</body>
</html>

Please let me know if you need clarification on anything.

-John

DanRumney

Posts: 205
Registered: Jul 20, 2007 01:32:39 PM
Re: Retrieve XML attribute values with AJAX
Posted: Nov 10, 2009 11:22:23 AM   in response to: johnjmk in response to: johnjmk's post
Click to report abuse...   Click to reply to this thread Reply
This code:
var myDataNode = myXML.documentElement;
 
var logonNode = myDataNode.firstChild;
var transNode = logonNode.firstChild;


looks suspect to me.

Your trans elements are not children of your logon element.
tsuji

Posts: 28
Registered: Sep 14, 2009 12:58:36 PM
Re: Retrieve XML attribute values with AJAX
Posted: Nov 11, 2009 10:28:28 AM   in response to: johnjmk in response to: johnjmk's post
Click to report abuse...   Click to reply to this thread Reply
In the myLogon=="Trans" section in question, you could use getElementsByTagName("tran") on myDataNode as you've used some other place. But if for some reason you want to start from logonName node, you can do it like this.
else if(myLogon == "Trans")
{
var myDataNode = myXML.documentElement;
 
//General use like this with .firstChild is valid only for ie. 
//For firefox etc, the ignorable whitespace is not ignored by default,
//the "child" counting would be different.
//Here, I suppose ie as a start; for cross-browser script, need some elaboration.
 
var logonNode = myDataNode.firstChild;
 
//start building the table and header lines etc
 
var transNode = logonNode.nextSibling;    //use nextSibling to traverse nodes
if (transNode!=null) {
  //here again suppose ignorable whitespace is ignored like ie,
  //otherwise, transNode might be a text node and you've to test nodeType first
  //before testing .tagName on element type.
  if (transNode.tagName=="tran") {
 
    //do the retrival of attribute values and building the table's tr/td etc etc...
 
  }
  transNode=transNode.nextSibling;
}
//finishing table building etc...
tsuji

Posts: 28
Registered: Sep 14, 2009 12:58:36 PM
Re: Retrieve XML attribute values with AJAX
Posted: Nov 11, 2009 12:36:33 PM   in response to: tsuji in response to: tsuji's post
Click to report abuse...   Click to reply to this thread Reply
amendment
The corresponding line should be read like this ("trans" rather than "tran"), obviously.
if (transNode.tagName=="trans") {
tsuji

Posts: 28
Registered: Sep 14, 2009 12:58:36 PM
Re: Retrieve XML attribute values with AJAX
Posted: Nov 11, 2009 10:27:28 PM   in response to: tsuji in response to: tsuji's post
Click to report abuse...   Click to reply to this thread Reply
amendment-2

Sorry for this major blunder when typing up reply. This line
if (transNode!=null) {
should be read
while (transNode!=null) {

Sorry for the confusion.
 Tags
Help

Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular type of content or application that you're viewing.

My tags shows your tags for this particular type of content or application that you're viewing.

 

MoreLess 


Point your RSS reader here for a feed of the latest messages in all forums