View Full Version : DOMParser()

06-17-2004, 10:07 PM
I've been trying to figure out how to load an XML Data island into a DOM for mozilla and some people have suggested using a DOMParser(). I tried looking up what a DOMParser() is exactly, but I can't seem to find anything consistent. When I look it up a bunch of different APIs come up, not a single one explaining how mozilla even knows about it. My guess is that Mozilla automatically loads those libraries, but where can I find a consistent reliable DOM API doc? so I can perhaps use the DOMParser the same way I am using Microsoft.XMLDOM. Thanks.

06-18-2004, 01:00 AM
DOMParser() is a constructor function. It has one particularly useful method, parseFromString(), which accepts a string of markup and a string declaring the mime-type of the document it came from, and returns an XMLDocument (or in theory whatever type of document is appropriate) consistent with what the string represented.

Alternative methods might be createContextualFragment() from a Range() (but it might be broken... I haven't worked with it in a while):

var range = document.createRange();
var fragment = range.createContextualFragment("the markup string");

It does appear to be broken, nevermind

06-18-2004, 02:55 PM
Is it safe to say that a DOMParser() object does not have the same functionality as an ActiveXObject? I mean I don't need that proprietary stuff, I just want to be able to create a DOM object and load an XML data island in and grab it out later using the DOM API.

I do something like this right now:

xmlDoc = (new DOMParser()).parseFromString(document.getElementsByTagName("xml")[0].innerHTML, 'text/xml');
hasFile = true;

where the <xml> is actually in the html code (as a data island). I mean it might be in a DOMParser after that code is run, but I can't seem to access that object the same way I can access a typical DOM object. Like xmlDoc.getElementsByTagName(' ').length;

I mean it works, but it keeps on returning "funny" things that i don't expect.

06-18-2004, 08:05 PM
I mean it works, but it keeps on returning "funny" things that i don't expect.

Once again, I must ask, "What funny things?"

In your other thread, DOMParser() returned precisely the XML document I was expecting. Could you clarify further by what you mean by funny?