473,750 Members | 2,213 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 5542
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
1882
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
2254
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 action on the selected rows. How can I use javascript to traverse through the checkboxes and count...
3
3399
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
1421
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 though. As this is my first try on C#, I would really like some input on my code, and especially...
2
3194
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 how many are checked. This is so that I can give them a confirmation dialog before I do an action...
5
1855
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 you get the idea). Class 1 '------------------
3
7205
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 like this: MyClass instance = new MyClass(); instance.property1 = "A string";
1
2058
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
3805
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
4424
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
9396
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9339
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9256
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6804
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4887
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
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
2
2804
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2225
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.