473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

traverse xml with dom

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
4 5529
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
Also whats the DOMParser object? I don't know wanything about nodes
and the likes.

Jan 18 '06 #3


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
Is it possible to traverse the values of children of an element, and no need to specify the children's element names in XSLT element? The XML file can be <bio> <skills> <languages>...
3
by: john | last post by:
I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the user clicks on a certain button on the page, which is not in the grid, I want to be able to...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
0
by: Egil Hansen | last post by:
Hi all I've written a class that will traverse a folder and its subfolders for every file that older then a specific time span. I do this through a recursive call to a method I call "Traverse",...
2
by: john | last post by:
I posted this question to comp.lang.javascript but didn't get a response, so I'll try here. I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the...
5
by: Bob Day | last post by:
Using vs 2003, vb.net It is not clear from the documentation, but it appears that a raised event will traverse up the call stack upwards until it reaches its handler. Is this correct? See the...
3
by: visor7 | last post by:
Hi, i've been trying to investigate how to traverse through an object instance properties or fields with no success. I have read about IEnumerable interface but that seems to apply only to...
1
by: SonOfGrey | last post by:
Hi! I'm looking for a way to traverse all the controls on a dialog in the order of the tabstop index. Note that I use unmanaged C++, so no GetTabIndex function afaik. The ALMOST perfect...
2
by: rvimalram | last post by:
Hi all, I have a created a multidimesional array,what i need is to traverse that array and return the value of the array in the particular page the array is $one=array("one" =>"Text1");...
6
by: gskbond | last post by:
Following is my logic to traverse level by level an mway search tree.... (Yeah I finally used templates to implement a queue... :) ) void mWayTree:: TraverseLevelOrder(Node *root,int lvl) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.