...

View Full Version : problem parsing xml, simple question (resolved)



Coastal Web
02-09-2008, 02:09 AM
Greetings folks,

I've attempting to parse in the contents of an xml file and l'm running into a bit of a wall... I'm trying to read data from that is a nodeType of 3 (text) ex: <name>textual value</name> but no matter what l do, actionscript thinks that the nodeType is 1 ex: <name src="textual value" />...

I've watered everythign down as much as possible to make it easy for anyone willing to take a look.

Here's my xml file:


<?xml version="1.0" encoding="iso-8859-1"?>
<list>
<property>
<title>title here</title>
<price>$200,000</price>
<desc>descripotion here.. descripotion here... descripotion here... descripotion here... </desc>
<img src="pic1.jpg" />
</property>
</list>


And here's the Actionscript l'm using:


xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("feed.xml");
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
trace(xmlNode.childNodes[i].childNodes[0].nodeValue);
trace(xmlNode.childNodes[i].childNodes[1].nodeValue);
trace(xmlNode.childNodes[i].childNodes[2].nodeValue);
trace(xmlNode.childNodes[i].childNodes[3].attributes.src);
}
} else {
trace('could not load xml file!');
}
}


I would expect the trace commands to output:



title here
$200,000
descripotion here.. descripotion here... descripotion here... descripotion here...
pic1.jpg


in this example, however the following is what l get:



null
null
null
pic1.jpg


Upon closer inspection, the actionscript thinks that the ladder three nodes are type 1, and note type 3 (text)... any idea how to fix this?
If someone could let me know what l'm doing wrong l would really appreciate it, l'm sure it's a mundane oversight on my part... :S

shyam
02-09-2008, 04:52 AM
xmlNode.childNodes[0].childNodes[0] reaches down to the title element and any element node's nodeValue is null. only text nodes have nodeValue defined so, changing to xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue should solve your problem

Coastal Web
02-09-2008, 06:03 AM
Awesome, shyam ...

did the trick perfectly... +karma and +thanks...

Warm regards,



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum