Jump to content

Javascript and XML


php_novice2007

Recommended Posts

Hi,

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:

var xml = GXml.parse(data);  
var units = xml.documentElement.getElementsByTagName("unit");  
for (var i = 0; i < units.length; i++) {
    var unitName = units[i].getAttribute("unitName");
    ????
}

 

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..

 

Please help me,

 

Thanks~!

 

 

 

 

 

 

Link to comment
Share on other sites

I figured something out :)

 

var unitList = xml.documentElement.getElementsByTagName("unit");
//goes through each unit
for (var i =0; i < unitList.length; i++) {
     var tmpArray = new Array();
     for( var x = 0; unitList[i].childNodes[x]; x++ ) {
         var posNode = unitList[i].childNodes[x];
         var point = new GLatLng(parseFloat(posNode.getAttribute("latitude")), 
                                    parseFloat(posNode.getAttribute("longitude")));
          tmpArray.push(point); 
          bounds.extend(point);
      }
      arrayOfarrayOfpoints.push(tmpArray);
}

 

But this code only works in IE.. in Firefox I get the error:

posNode.getAttribute is not a function

 

Does anyone know how I can make my page work in Firefox?

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.