Reputation: 412
I'm trying to parse xml from an notes.xml, it show an error in firebug as
TypeError: xml.getElementsByTagName is not a function
My code part is,
notes.xml
<fr>
<franchise city="Scottsdale" state=" AZ" />
<franchise city="Arcadia" state=" CA" />
</fr>
javascript
<script>
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","notes.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("franchise");
alert(x.getElementsByTagName("state")[0].childNodes[0].nodeValue);
</script>
Upvotes: 0
Views: 5646
Reputation: 11258
Your alert statement is wrong. x
has no method getElementsByTagName
.
You can get the first city using:
alert(x[0].attributes[0].nodeValue); // shows Scottsdale
The second one is:
alert(x[1].attributes[0].nodeValue); // shows Arcadia
And states:
alert(x[0].attributes[1].nodeValue); // AZ
alert(x[1].attributes[1].nodeValue); // CA
Upvotes: 1