I know there are many questions and articles discussing this issue on the internet, but I somehow can’t get this to work. I’m quite sure I’m missing something basic, but I can’t find it.
The parsing itself:
var str="<article>Some article</article><other>Other stuff</other>";
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(str,"text/xml");
}
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject ("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.loadXML(str);
}
var node = xmlDoc.getElementsByTagName("article")[0].childNodes[0].nodeValue;
alert (node);
But it doesn’t work, FF says that:
xmlDoc.getElementsByTagName("article")[0] is undefined
Also, it works if I use str like this:
var str="<article>Some article</article>";
So the question is, why doesn’t it work? Parsing doesn’t work right even if I append just one character to the end of str variable. Could you also point me out to some useful tutorial regarding this behaviour?
Your string isn’t valid XML since it has multiple root nodes. Did you mean something like: