I’m creating a Windows 8 JavaScript app, and I need to convert a string somewhat like this:
<p>
blah blah blah
</p>
<div>
<p>
random dom stuff
</p>
</div>
Into an XML DOM object, so I can traverse it with DOM methods (ie getElementByID()).
I’ve tried two ways
//retrieve text to process
var content = xml.querySelector("api > parse > text").textContent;
//1
var contentXML = new DOMParser().parseFromString(content, "text/xml");
//2
var newContentXML = new ActiveXObject("Microsoft.XMLDOM");
newContentXM.async = false;
newContentXM.loadXML(content);
Both fail. #1 with Only one root element is allowed., and #2 with Automation server can't create object. Can't load the ActiveX plug-in that has the class ID '{2933BF90-7B36-11D2-B20E-00C04F983E60}'. Applications can't load ActiveX controls.
Everywhere I’ve looked says #2 is how you should do it in IE, and I’m presuming W8 JS apps use the same JavaScript engine as IE.
How can I convert my text to an XML DOM object?
DOMParser is fine, and the error is accurate. parseFromString returns an instance of a document, but your text doesn’t define a single document root, it’s a concatenation of two: a paragraph and a div.
The following will work:
By the way with your original string,