Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: DOMParser()

  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DOMParser()

    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.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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");

    Edit: It does appear to be broken, nevermind

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    alert(xmlDoc);

    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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Quote Originally Posted by deadseasquirrel
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •