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

traverse xml with dom

P: n/a
Hi there!
I'm trying to figure out how to traverse an xml hierarchy from within
javascript.
Here's my text.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<main>
<text1>this is a text</text1>
<text2>this is another one</text2>
<text3>and this is the last</text3>
</main>

And here's the xhtml/javascript code I've written. It's supposed to be
viewed in mozilla/firefox (now I want a working function, then I'll
think about cross-browsing stuff), and it doesn't work as I expected:

<html>
<title>temp</title>
<head>
<script name="text/javascript">
<!--
var xml_dom = null;

window.onload=function() {
xml_dom = document.implementation.createDocument("", "", null);
xml_dom.load("text.xml");
xml_dom.onload=parse_xml;
}

function parse_xml() {
var xml_parser = new DOMParser();
xml_dom = xml_parser.parseFromString("<main>", "text/xml");

var nodes = xml_dom.documentElement.childNodes;
alert(nodes.length); // this say "2", but it should be "3"...why???
for (var i=0; i < nodes.length; i++) {
document.write(nodes[i].nodeValue);
}
}
//-->
</script>
</head>

<body>
</body>
</html>

What am I doing wrong??
Thanks,

shinya
Jan 18 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
you could you E4X. It only works in FF right now but its a W3C
standard I beleive.
http://www.w3schools.com/e4x/default.asp

Jan 18 '06 #2

P: n/a
Also whats the DOMParser object? I don't know wanything about nodes
and the likes.

Jan 18 '06 #3

P: n/a


mo********@gmail.com wrote:
you could you E4X. It only works in FF right now but its a W3C
standard I beleive.
No, E4X is not a W3C standard, it is an ECMA standard and currently on
its way to an ISO standard.
But E4X as specified and implemented in Firefox 1.5 does not help to
load and parse XML from a URL.
You can only build XML objects natively or parse from string. Hopefully
the next major new Firefox release will have DOM <--> E4X integration
but so far there is no such integration.
http://www.w3schools.com/e4x/default.asp


That claims that

var xmlDoc=new XML()
xmlDoc.load("note.xml")

is (possible with) E4X but that is nonsense, the XML objects in E4X do
not have any method named load.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 18 '06 #4

P: n/a
shinya wrote:
Hi there!
I'm trying to figure out how to traverse an xml hierarchy from within
javascript.
Here's my text.xml file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<main>
<text1>this is a text</text1>
<text2>this is another one</text2>
<text3>and this is the last</text3>
</main>

And here's the xhtml/javascript code I've written. It's supposed to be
viewed in mozilla/firefox (now I want a working function, then I'll
think about cross-browsing stuff), and it doesn't work as I expected:

<html>
<title>temp</title>
<head>
<script name="text/javascript">
<!--
var xml_dom = null;

window.onload=function() {
xml_dom = document.implementation.createDocument("", "", null);
xml_dom.load("text.xml");
xml_dom.onload=parse_xml;
}

function parse_xml() {
var xml_parser = new DOMParser();
xml_dom = xml_parser.parseFromString("<main>", "text/xml"); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^

Why are you reassigning xml_dom?

var nodes = xml_dom.documentElement.childNodes;
alert(nodes.length); // this say "2", but it should be "3"...why???
for (var i=0; i < nodes.length; i++) {
document.write(nodes[i].nodeValue);
}
}
//-->
</script>
</head>

<body>
</body>
</html>


-- David

Jan 18 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.