XML Newbie Madness

Discussion in 'XML' started by Benoit, Apr 12, 2008.

  1. Benoit

    Benoit Guest

    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>Student</occupation>
    </tenant>
    <tenant>
    <name>
    <first>Alan</first>
    <last>Smithee</last>
    </name>
    <age>22</age>
    <occupation>Server</occupation>
    </tenant>
    <tenant>
    <name>
    <first>Jane</first>
    <last>Smith</last>
    </name>
    <age>34</age>
    <occupation>Manager</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.implementation.createDocument("","", null);
    xmlDoc.async = "false";
    xmlDoc.load("Apartment.xml");

    document.getElementById("fname").innerHTML =
    xmlDoc.getElementsByTagName("name")[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.getElementsByTagName("name")[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?
    Benoit, Apr 12, 2008
    #1
    1. Advertising

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

    You can't Validate without a DTD or Schema. Your document doesn't
    specify either; ergo, validation is not an (ahem) valid operation. (At
    least, not unless you use some other mechanism to tell the parser what
    to validate it against.)

    Without those, all you can test it for is well-formedness.

    Also, without those there is no way to tell which attributes are or are
    not IDs, so getElementById is useless to you.

    Also, note that per the DOM spec, an Element has no nodeValue. It's the
    application code's responsibity to extract a meaningful value. (The
    XPath convention is that an element's value is the concatenation of all
    its Text descendants; DOM Level 3 offers a call specifically to retrieve
    that value, but of course the question then becomes whether that's what
    you want and whether the DOM you're working with supports that feature.


    So far I've avoided javascript, so I'd rather not try to guess your
    issues on that front. I will point out that "innerHTML" isn't part of
    the standard W3C DOM API. I believe it's a Microsoftism, and should
    probably be avoided if you care about portability.

    The w3schools website has a... questionable ... reputation as far as
    accuracy and focus on standards are concerned. You might want to look
    for other tutorials.
    Joseph J. Kesselman, Apr 12, 2008
    #2
    1. Advertising

  3. (Note that the answers are slightly different for HTML DOMs, where there
    is a presumed DTD hardcoded into the DOM. XML is not HTML. HTML is not
    XML, though XHTML is. The DOM APIs are designed to work with either, but
    some behaviors differ depending on whether you're using a DOM which is
    specifically coded for HTML or not.)
    Joseph J. Kesselman, Apr 13, 2008
    #3
  4. Benoit

    The Magpie Guest

    Joseph J. Kesselman wrote:
    >> cvc-elt.1: Cannot find the declaration of element 'apartment'.

    >
    > You can't Validate without a DTD or Schema. Your document doesn't
    > specify either; ergo, validation is not an (ahem) valid operation. (At
    > least, not unless you use some other mechanism to tell the parser what
    > to validate it against.)
    >

    Also, you got hold of the element with the ID "fname" - and there
    isn't one. After that, I am not too convinced your use of the elemnts
    with the name "name" are handled properly and they may not have all
    the properties you are using.

    Take a look in the debugger and see what you are getting back.
    The Magpie, Apr 14, 2008
    #4
    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. Todd

    Session Variable Madness

    Todd, Jan 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    417
  2. V. Jenks

    RadioButtonList madness!

    V. Jenks, Apr 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    453
    Ashish M Bhonkiya
    Apr 19, 2004
  3. =?Utf-8?B?QUNhdW50ZXI=?=

    Audio Madness @#!#@

    =?Utf-8?B?QUNhdW50ZXI=?=, Feb 26, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    =?Utf-8?B?QUNhdW50ZXI=?=
    Feb 26, 2005
  4. Marius Horak

    Madness, I call it madness

    Marius Horak, Jun 4, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    149
    Raterus
    Jun 4, 2004
  5. Benoit

    XML Parsing Newbie Madness

    Benoit, Apr 12, 2008, in forum: Javascript
    Replies:
    7
    Views:
    165
Loading...

Share This Page