Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XML Newbie Madness

Reply
Thread Tools

XML Newbie Madness

 
 
Benoit
Guest
Posts: n/a
 
      04-12-2008
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?



 
Reply With Quote
 
 
 
 
Joseph J. Kesselman
Guest
Posts: n/a
 
      04-12-2008
> 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.
 
Reply With Quote
 
 
 
 
Joseph J. Kesselman
Guest
Posts: n/a
 
      04-13-2008
(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.)
 
Reply With Quote
 
The Magpie
Guest
Posts: n/a
 
      04-14-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Parsing Newbie Madness Benoit Javascript 7 04-15-2008 09:12 AM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
OT: Monday Madness =?iso-8859-1?Q?Frisbee=AE?= MCSE 15 03-22-2005 06:47 PM
Madness, I call it madness Marius Horak ASP .Net Datagrid Control 2 06-04-2004 02:28 PM
Cisco PIX 501 madness ... help stevo321 Cisco 5 04-11-2004 10:02 AM



Advertisments