473,549 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=f unction() {
xml_dom = document.implem entation.create Document("", "", null);
xml_dom.load("t ext.xml");
xml_dom.onload= parse_xml;
}

function parse_xml() {
var xml_parser = new DOMParser();
xml_dom = xml_parser.pars eFromString("<m ain>", "text/xml");

var nodes = xml_dom.documen tElement.childN odes;
alert(nodes.len gth); // 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 5533
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********@gmai l.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("no te.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=f unction() {
xml_dom = document.implem entation.create Document("", "", null);
xml_dom.load("t ext.xml");
xml_dom.onload= parse_xml;
}

function parse_xml() {
var xml_parser = new DOMParser();
xml_dom = xml_parser.pars eFromString("<m ain>", "text/xml"); ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

Why are you reassigning xml_dom?

var nodes = xml_dom.documen tElement.childN odes;
alert(nodes.len gth); // 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
1877
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> <language>XML</language>
3
2245
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 traverse through all the checkboxes in that column and see how many are checked. This is so that I can give them a confirmation dialog before I do an...
3
3374
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 please help me? TIA. -- <?php echo 'Just another PHP saint'; ?> Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
0
1414
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", but I saw somewhere in the newsgroups, that there is a way, that will make use of Windows's Indexing Service. I just wasn't able to figure out how...
2
3182
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 user clicks on a certain button on the page, which is not in the grid, I want to be able to traverse through all the checkboxes in that column and see...
5
1847
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 code snippet below. The event raised in sub1 or class three will be caught in Class1 event handler (I know below is not syntactically correct, but...
3
7201
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 collection of objects. What i want to do actually is to traverse through each property of an instance and if possible, assign a null value... something...
1
2050
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 function for this looks like: CWnd::GetNextDlgTabItem. BUT...this function skips controls that are disabled (WS_DISABLED),
2
3797
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"); $two=array( "two" =>array( "a" =>array( "b" =>"Text2"
6
4413
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) { int i; int level; Dq<Node *> Q; Node *tmp;
0
7471
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7985
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7830
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1082
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.