Question

I am trying to get a node from XML using DOMParser. The code below works in Chrome, but not in FF. Am I doing something wrong? Or, is FF doing something wrong? A fiddle here: http://jsfiddle.net/lborgman/D8QHT/2/

var zXml = '<?xml version="1.0"?> <div class="csl-bib-body" style="line-height: 2; padding-left: 2em; text-indent:-2em;"> <div class="csl-entry">The Polyvagal Theory.</div> </div>';
var dp = new DOMParser();
var zDom = dp.parseFromString(zXml, "text/xml");
document.getElementById("output").appendChild(document.createTextNode(zDom.nodeValue));
var divCite = zDom.querySelector("div.csl-entry");
console.log(divCite);
document.getElementById("output2").appendChild(document.createTextNode(divCite.innerHTML));
Was it helpful?

Solution

Try parsing as text/html: the querySelector doesn't work on an XML document because XML doesn't understand CSS classes.

var zDom = dp.parseFromString(zXml, "text/html");

Alternatively, you can use XML and querySelector, but be more explicit with your selector argument:

var divCite = zDom.querySelector("div[class=csl-entry]");

As for the nodeValue, I'm not sure what you expect to see in that property of a document object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top