[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

11/03/2009 at 08:37PM PST, ID: 24869781 | Points: 500
[x]
Attachment Details

Retrieve XML attributes with AJAX

Asked by johnjmk in Asynchronous Javascript and XML (AJAX), Extensible Markup Language (XML), JavaScript

Tags: AJAX, XML, JavaScript

I'm having trouble retrieving the attributes from some XML elements and displaying them in a table. This is just a slight variation on one of my previous assignments which I asked about before
(http://www.experts-exchange.com/Programming/Languages/Scripting/AJAX/Q_24788420.html)
This assignment is very similar except for one difference: when I choose "checking" or "savings" from the select box I am displaying the attributes of each element rather than the element itself.

The XML retrieved is this:

<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>

My problem is that on line 100 I keep getting the error "attributes is NULL". Just like the last program I am probably overlooking some minuscule detail but please help if you can.

All my classes are online, so I have little to no interaction with the teachers so this is my main source for help.

P.S. I would give you the URL of the program but I am currently unable to access my account on my teacher's website
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
<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;
	            	obj.innerHTML = "";
	            	displayXML(myXML);
	            }
	          } 
	          XMLHttpRequestObject.send("data=" + data);
	    } 
	    return false;
	}  
	function displayXML(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[i].firstChild.data + "> \
								 " + XMLCategoryElements[i].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>";
		} 
		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; 
			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>" + XMLTransElements[rows].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"> 
<div id="logon_form"> 
<h2>Logon</h2> 
<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><br><input type=submit value="Logon"
     onclick="return validateLogon('http://ntowl.com/testfiles/AJAXPHP/validate_logonxml_accounts.php', 'targetDiv')">
 </tr> 
</table>
</form>
</div> 
<div id="AcctInfo"></div> 
<div id="targetDiv"></div>  
</body>
</html>
[+][-]11/03/09 08:53 PM, ID: 25736496

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/03/09 09:51 PM, ID: 25736737

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/03/09 10:06 PM, ID: 25736804

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/04/09 12:22 AM, ID: 25737342

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/04/09 12:51 AM, ID: 25737468

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 05:41 AM, ID: 25749285

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/06/09 12:42 AM, ID: 25757570

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 - Hierarchy / EE_QW_3_20080625