XML Parsing Newbie Madness

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benoit

    XML Parsing Newbie Madness

    I've been instructing myself in XML DOM parsing using the w3schools
    tutorial and decided to try an example of my own. I'd written a short
    XML file that looked like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <apartment>
    <tenant>
    <name>
    <first>John</first>
    <last>Smith</last>
    </name>
    <age>23</age>
    <occupation>Stu dent</occupation>
    </tenant>
    <tenant>
    <name>
    <first>Alan</first>
    <last>Smithee </last>
    </name>
    <age>22</age>
    <occupation>Ser ver</occupation>
    </tenant>
    <tenant>
    <name>
    <first>Jane</first>
    <last>Smith</last>
    </name>
    <age>34</age>
    <occupation>Man ager</occupation>
    </tenant>
    </apartment>

    I wanted to parse the xml dom with javascript and insert node values
    into html elements at load calling the following function:

    function parseXML()
    {
    xmlDoc = document.implem entation.create Document("","", null);
    xmlDoc.async = "false";
    xmlDoc.load("Ap artment.xml");

    document.getEle mentById("fname ").innerHTM L =
    xmlDoc.getEleme ntsByTagName("n ame")[0].childNodes[0].nodeValue;
    }

    When I tested to see the resulting output in Safari and Firefox, I
    received to different error messages:

    1) Safari 3.1's Inspector error console told me: value undefined Value
    undefined (result of expression xmlDoc.load) is not object.
    2) Firebug in Firefox told me: xmlDoc.getEleme ntsByTagName("n ame")[0]
    has no properties

    I decided to ignore Safari and focus on the Firefox bug first. I
    deleted the last statement of the function just to make sure the xml
    file was being loaded. However, no matter where I parsed the tree for
    a node value, I was told the node had no value. So I decided to
    validate my XML using the XML Developer extension for Firefox and ran
    the XML through its validator (it used some default scheme when none
    was provided) and I was told that:

    cvc-elt.1: Cannot find the declaration of element 'apartment'.

    Huh? But its right there! Am I doing something wrong?


  • pr

    #2
    Re: XML Parsing Newbie Madness

    Benoit wrote:
    I've been instructing myself in XML DOM parsing using the w3schools
    tutorial and decided to try an example of my own. I'd written a short
    XML file that looked like this:
    >
    [...]

    It checks out.
    >
    I wanted to parse the xml dom with javascript and insert node values
    into html elements at load calling the following function:
    >
    function parseXML()
    {
    xmlDoc = document.implem entation.create Document("","", null);
    Don't forget *var*.
    xmlDoc.async = "false";
    I don't believe Firefox lets you do this, so you need an event listener
    to intercept a successful load(), or to use an XMLHttpRequest.
    xmlDoc.load("Ap artment.xml");
    >
    document.getEle mentById("fname ").innerHTM L =
    xmlDoc.getEleme ntsByTagName("n ame")[0].childNodes[0].nodeValue;
    That isn't quite right. nodeValue only applies to text nodes. Firefox
    2.x (at least) supports textContent on element nodes, however.

    Putting it all together, you can replace those two lines with:

    xmlDoc.addEvent Listener("load" , loaded, false);

    function loaded(e) {
    document.getEle mentById("fname ").innerHTM L =
    xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;
    }

    xmlDoc.load("Ap artment.xml");
    }
    >
    When I tested to see the resulting output in Safari and Firefox, I
    received to different error messages:
    >
    1) Safari 3.1's Inspector error console told me: value undefined Value
    undefined (result of expression xmlDoc.load) is not object.
    2) Firebug in Firefox told me: xmlDoc.getEleme ntsByTagName("n ame")[0]
    has no properties
    >
    I decided to ignore Safari and focus on the Firefox bug first. I
    deleted the last statement of the function just to make sure the xml
    file was being loaded. However, no matter where I parsed the tree for
    a node value, I was told the node had no value. So I decided to
    validate my XML using the XML Developer extension for Firefox and ran
    the XML through its validator (it used some default scheme when none
    was provided) and I was told that:
    >
    cvc-elt.1: Cannot find the declaration of element 'apartment'.
    >
    Huh? But its right there! Am I doing something wrong?
    >
    You don't want to get into serious XML validation at this point, believe
    me :-)

    Comment

    • pr

      #3
      Re: XML Parsing Newbie Madness

      pr wrote:
      Benoit wrote:
      > xmlDoc.async = "false";
      >
      I don't believe Firefox lets you do this, so you need an event listener
      to intercept a successful load(), or to use an XMLHttpRequest.
      Argh, you confused me for a minute there. Of course async doesn't work
      if it's a string value. What you wanted was:

      xmlDoc.async = false;

      Comment

      • pr

        #4
        Re: XML Parsing Newbie Madness

        VK wrote:
        On Apr 13, 1:23 am, pr <p...@porl.glob alnet.co.ukwrot e:
        > xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;
        >
        Right, I forgot that DOM 3 finally introduced .textContent as
        equivalent to IE's .text and it is finally mostly supported. So the
        cross-browser code might be like:
        >
        [...]

        For that simple example maybe. For serious purposes you would IMO use
        XMLHttpRequest asynchronously, supply an alternative to textContent/text
        and avoid innerHTML. Safari 3.1 wouldn't have any difficulty with that.

        Comment

        • Benoit

          #5
          Re: XML Parsing Newbie Madness

          Seeing as the W3schools tutorial is misleading, could suggest a text
          on XML technologies that's modern and browser-agnostic if not browser-
          sensitive?

          On Apr 12, 5:44 pm, pr <p...@porl.glob alnet.co.ukwrot e:
          VK wrote:
          On Apr 13, 1:23 am, pr <p...@porl.glob alnet.co.ukwrot e:
          xmlDoc.getEleme ntsByTagName("n ame")[0].textContent;
          >
          Right, I forgot that DOM 3 finally introduced .textContent as
          equivalent to IE's .text and it is finally mostly supported. So the
          cross-browser code might be like:
          >
          [...]
          >
          For that simple example maybe. For serious purposes you would IMO use
          XMLHttpRequest asynchronously, supply an alternative to textContent/text
          and avoid innerHTML. Safari 3.1 wouldn't have any difficulty with that.

          Comment

          • pr

            #6
            Re: XML Parsing Newbie Madness

            Benoit wrote:
            Seeing as the W3schools tutorial is misleading, could suggest a text
            on XML technologies that's modern and browser-agnostic if not browser-
            sensitive?
            >
            Here are a few links:

            Another very common task in modern websites and applications is making network requests to retrieve individual data items from the server to update sections of a webpage without having to load an entire new page. This seemingly small detail has had a huge impact on the performance and behavior of sites, so in this article, we'll explain the concept and look at technologies that make it possible: in particular, the Fetch API.

            XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing.

            The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps.




            On the subject of the version 2.0 DOM (for navigating and updating HTML
            & XML content), there's nothing better than:



            (once you get over the 'officialese').

            You could also look at the Apple site, for example:



            Microsoft has some good documentation on its MSXML SDK:



            but you have to bear in mind that IE doesn't support XHTML and therefore
            interaction between HTML and XML documents in IE is carried out by
            rather primitive means (innerHTML instead of importNode(), for example).

            Hope that helps.

            Comment

            Working...