By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,549 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,549 IT Pros & Developers. It's quick & easy.

XML Parsing Newbie Madness

P: n/a
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?
Jun 27 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
pr
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.implementation.createDocument("","", 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("Apartment.xml");

document.getElementById("fname").innerHTML =
xmlDoc.getElementsByTagName("name")[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.addEventListener("load", loaded, false);

function loaded(e) {
document.getElementById("fname").innerHTML =
xmlDoc.getElementsByTagName("name")[0].textContent;
}

xmlDoc.load("Apartment.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.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?
You don't want to get into serious XML validation at this point, believe
me :-)
Jun 27 '08 #2

P: n/a
pr
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;
Jun 27 '08 #3

P: n/a
pr
VK wrote:
On Apr 13, 1:23 am, pr <p...@porl.globalnet.co.ukwrote:
> xmlDoc.getElementsByTagName("name")[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.

Jun 27 '08 #4

P: n/a
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.globalnet.co.ukwrote:
VK wrote:
On Apr 13, 1:23 am, pr <p...@porl.globalnet.co.ukwrote:
xmlDoc.getElementsByTagName("name")[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.
Jun 27 '08 #5

P: n/a
pr
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:

http://developer.mozilla.org/en/docs...etting_Started
http://developer.mozilla.org/en/docs/XMLHttpRequest
http://developer.mozilla.org/en/docs..._DOM_Reference

http://jibbering.com/2002/4/httprequest.html

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

http://www.w3.org/TR/DOM-Level-2-Core

(once you get over the 'officialese').

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

http://developer.apple.com/internet/...ent/dom2i.html

Microsoft has some good documentation on its MSXML SDK:

http://msdn2.microsoft.com/en-us/library/ms760399.aspx

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.
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.