PDA

View Full Version : Parsing XML


w0lf42
04-28-2006, 05:21 PM
I am attempting to retrieve data from an XML file and populate it in an XHTML file (yes, this is AJAX).

The problem seems to be that JavaScript reports the nodeType as 1 - an HTML element - and not text.

Any chance someone would be able to review the function I wrote which parses the XML and outputs it into an unordered list on the page?

function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var response = xmlHttp.responseXML.documentElement;

var navItems = response.getElementsByTagName("navitem")

var outputData = '<ul>';
//alert(navItems.length); // returns 6 - the number of navitem elements in the xml file
for (var i = 0; i < navItems.length; i++) {
// navItems[i].nodeValue returns null
// navItems[i].nodeType returns 1 - an HTML element
// type 3 returns text - and nodeValue is only applicable to text nodes
outputData += ('<li>' + navItems[i].nodeValue + '</li>');
}
outputData += '</ul>';
document.getElementById('subNavigation').innerHTML = outputData;
}
}



XML File (http://markschamel.com/ajax.navigation.xml)
XHTML/JavaScript Filel (http://markschamel.com/ajax.navigation.html)

w0lf42
04-28-2006, 10:34 PM
I have figured this one out. If anyone is interested, the code that was failing needed to have a ".firstChild" added to it.

This:
outputData += ('<li>' + navItems[i].nodeValue + '</li>');
Needed to change to:
outputData += ('<li>' + navItems[i].firstChild.nodeValue + '</li>');

Here is the code:
function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var response = xmlHttp.responseXML.documentElement;

var navItems = response.getElementsByTagName("navitem")

var parentNavItems = response.getElementsByTagName("response")

var outputData = '<ul>';
//alert(navItems.length); // returns 6 - the number of navitem elements in the xml file
for (var i = 0; i < navItems.length; i++) {
hrefAttribute = navItems[i].firstChild.attributes;
outputData += ('<li>' + navItems[i].firstChild.nodeValue + '</li>');
}

outputData += '</ul>';
document.getElementById('subNavigation').innerHTML = outputData;
}
}
}

This is succesfully parsing my XML!

w0lf42
05-01-2006, 02:09 PM
I'm really pleased with how this script is coming along. However, in Firefox I am getting errors if I mouse over the links too quickly.

There are two lines of code that are returning errors:
if (xmlHttp.status == 200) {
Which returns this error message:
Error: Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE) [nsIXMLHttpRequest.status]

and
xmlHttp.send(null);

Which returns this error message:
Error: Component returned failure code: 0xc1f30001 (NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]

Any suggestions?

Jon Hanlon
05-02-2006, 06:38 PM
Need to see more of your code around

xmlHttp.send(null);

w0lf42
05-02-2006, 06:52 PM
Here is all of the JavaScript code. Thank you for looking into this.

/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
@end @*/

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}

function updatePage() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var response = xmlHttp.responseXML.documentElement;

var navItems = response.getElementsByTagName("navitem")

var parentNavItems = response.getElementsByTagName("response")

var outputData = '<ul>';
//alert(navItems.length); // returns 6 - the number of navitem elements in the xml file
for (var i = 0; i < navItems.length; i++) {
//hrefAttribute = navItems[i].firstChild.attributes;
outputData += ('<li>' + navItems[i].firstChild.nodeValue + '</li>');
}

outputData += '</ul>';
document.getElementById('subNavigation').innerHTML = outputData;
}
}
}

function callServer(sectionName) {
// Build the URL to connect to
//var url = "ajax.navigation.xml";
var url = "ajax.navigation.php?sectionName=" + escape(sectionName);

// Open a connection to the server
xmlHttp.open("GET", url, true);

// Setup a function for the server to run when it's done
xmlHttp.onreadystatechange = updatePage;

// Send the request
xmlHttp.send(null);
}

w0lf42
05-12-2006, 03:09 PM
I've been searching all of the net looking for answers to this and have come up short.

Anything that you can help me with would be of great help.

RysChwith
05-12-2006, 05:26 PM
Judging from the errors, it's looking like the HTTPRequest object isn't being created properly. I have to admit that your code for that is a little odd. Give this one a shot:function getRequestObject() {
var reqObj;

if( window.XMLHttpRequest ) {
reqObj = new XMLHttpRequest();
} else if( window.ActiveXObject ) {
var msxmls = new Array( "Msxml2.XMLHTTP.5.0",
"Msxml2.XMLHTTP.4.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP" );

for( var i = 0; i < msxmls.length; i++ ) {
try {
reqObj = new ActiveXObject( msxmls[ i ] );
} catch( e ) {
}
}
} else {
return false;
}

return reqObj;
}It's set up as a function that you call like so:var reqObj = getRequestObject();but you could just as easily work it into your code.

Rys

Jon Hanlon
05-14-2006, 07:54 PM
You'll run into problems if a send is already in progress.

if (xmlHttp.readyState > 0 && xmlHttp.readyState < 4) xmlHttp.abort(); // kill if 1,2,3
xmlHttp.send(null);