XML Newbie Madness

B

Benoit

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?
 
J

Joseph J. Kesselman

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.
 
J

Joseph J. Kesselman

(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.)
 
T

The Magpie

Joseph said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top