Jump to content

Parsing XML


mentalist

Recommended Posts

Hi,

 

I've found a few examples and references which all state this should work:

<script>
s='	\
<fruits>	\
  <fruit name="Apple" colour="Green" />	\
  <fruit name="Banana" colour="Yellow" />	\
</fruits>	\
';

function gen_xml(s){
if (window.DOMParser)	{
	parser=new DOMParser();
	xmlDoc=parser.parseFromString(s,"text/xml");
}
else{ // Internet Explorer
	xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.loadXML(s);
}
return xmlDoc;
}

function getFruits(xml){
var fruits = xml.getElementsByTagName("fruits")[0];
if (fruits) {
	var fruitsNodes = fruits.childNodes;
	if (fruitsNodes){
		for (var i = 0; i < fruitsNodes.length; i++){
			var name = fruitsNodes[i].getAttribute("name");
			var colour = fruitsNodes[i].getAttribute("colour");
			alert("Fruit " + name + " is coloured " + colour);
		}
	}
}
}

xml=gen_xml(s);
getFruits(xml);
</script>

 

But I keep getting the following error:

Error: fruitsNodes[i].getAttribute is not a function

 

Cheers!

Link to comment
https://forums.phpfreaks.com/topic/237811-parsing-xml/
Share on other sites

Thankyou, that's a great help and shed some light as to why.

 

I'd used this way by the end of last night (I'll add just for examples):

fruitsNodes=xml.getElementsByTagName('fruit');
for (i=0;i<fruitsNodes.length;i++){
	var name = fruitsNodes[i].getAttribute("name");
	var colour = fruitsNodes[i].getAttribute("colour");
	alert("Fruit " + name + " is coloured " + colour);
}

 

Slight changes, yet basically the same, but it's just the loop type from what I can see...

Link to comment
https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222290
Share on other sites

It is the same type of loop,  but you are basically looping over a different set of elements.

 

In your original, you were looping over all the childrenNodes of xml.getElementsByTagName("fruits")[0], which included text nodes and other what-nots, including children elements without <fruit> tags. If I have a <citrous> element under <fruits>, the original piece of code will also be able to access the <citrous> element.

 

In your new piece of code, you are specifically looking for elements with the tag "fruit". This is probably faster if you simply want to find only elements with the <fruit> tag.

 

PS: Do remember to mark the topic as "Solved" too!

Link to comment
https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222296
Share on other sites

Cheers!

 

I'd investigated, checking for what was there using:

var o;
for(o in fruitsNodes[i].childNodes){
try
{
	desc = fruitsNodes[i].childNodes[o];
}
catch(e)
{
	desc = "ERROR";
}
alert("o: " + o + ", desc: " + desc);
}

 

I'd say it's a bit like Python's 'dir' function for javascript.

Link to comment
https://forums.phpfreaks.com/topic/237811-parsing-xml/#findComment-1222312
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.