Read entire xml file

Discussion in 'Javascript' started by Sunny, Oct 6, 2008.

  1. Sunny

    Sunny Guest

    Hi,
    Can someone tell me how to get the content of entire xml file in
    Firefox?
    I am using this code to load the xml file.
    if (document.implementation && document.implementation.createDocument)
    {
    xmlDoc = document.implementation.createDocument("","",null);
    xmlDoc.async="false";
    var isLoaded = xmlDoc.load("allcounties_3.xml");
    if (isLoaded == true) {
    //alert(xmlDoc.documentElement.xml);
    }
    }

    I tried xmlDoc.documentElement.xml but it dont work.
    How can I read the entire xml file in a variable?
     
    Sunny, Oct 6, 2008
    #1
    1. Advertising

  2. Sunny wrote:

    > I am using this code to load the xml file.
    > if (document.implementation && document.implementation.createDocument)
    > {
    > xmlDoc = document.implementation.createDocument("","",null);
    > xmlDoc.async="false";


    You should assign a boolean value not a string to the async property:
    xmlDoc.async = false;
    However that blocks the browser while loading the document so that is
    usually not a good idea. Consider to use asynchronous loading (async =
    true) in a browser environment.

    > var isLoaded = xmlDoc.load("allcounties_3.xml");
    > if (isLoaded == true) {
    > //alert(xmlDoc.documentElement.xml);
    > }
    > }
    >
    > I tried xmlDoc.documentElement.xml but it dont work.
    > How can I read the entire xml file in a variable?


    Well xmlDoc is a variable containing the XML document as a DOM tree.
    If you only want a string then I am not sure why you use
    createDocument() and the load method, you could use XMLHttpRequest and
    simply access responseText.
    Or, if you think you need createDocument() and the load method, then you
    need to serialize the DOM tree e.g.
    var xml = new XMLSerializer().serializeToString(xmlDoc);

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 6, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ETL
    Replies:
    9
    Views:
    852
  2. Jeremy
    Replies:
    3
    Views:
    2,837
    John Machin
    Jul 15, 2005
  3. David Southwell
    Replies:
    3
    Views:
    166
    Phrogz
    Nov 4, 2009
  4. PerlFAQ Server
    Replies:
    0
    Views:
    108
    PerlFAQ Server
    Jan 26, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    124
    PerlFAQ Server
    Mar 16, 2011
Loading...

Share This Page