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
https://forums.phpfreaks.com/topic/63500-javascript-and-xml/
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
https://forums.phpfreaks.com/topic/63500-javascript-and-xml/#findComment-317240
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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