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

Reading XML

P: 1
Hi,

I'm not sure whether this goes in the XML or the Javascript forum but I guess its more Javascript because I want to read an XML file..

Anyway... I've got an XML file in this form:

<units>
<unit unitName=...>
<position time=... lat=... long=.../>
<position time=... lat=... long=.../>
...
</unit>
<unit unitName=...>
<position time=... lat=... long=.../>
<position time=... lat=... long=.../>
....
</unit>
....
</units>

And I want to write Javascript to extract these information.

So far I've got:

Expand|Select|Wrap|Line Numbers
  1. var xml = GXml.parse(data);  
  2. var units = xml.documentElement.getElementsByTagName("unit");  
  3. for (var i = 0; i < units.length; i++) {
  4.     var unitName = units[i].getAttribute("unitName");
  5.     ????
  6. }
  7.  
In ???? I want to get a list of the "position" tags within that unit, and extract the latitude and longitude values. But I'm not sure how to do it. I tried getElementsByTagName but I just got all the position tags in the xml file and I don't know which unit they belong to.. I was reading some tutorials and they mentioned methods like firstChild and lastChild and nextSibling but I'm not sure how to use them. Maybe I can get out the firstChild (How would I check IF there is a firstChild?) and then have some sort of loop which goes until it is the lastChild? I can't seem to find a method which checks if there is a next child.. I don't know the syntax..

Thanks for any help~!
Aug 6 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
You can use units[i].getElementsByTagName("position") instead to get all position elements within a particular unit.
Nov 3 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.