php_novice2007 Posted August 6, 2007 Share Posted August 6, 2007 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~! Quote Link to comment Share on other sites More sharing options...
php_novice2007 Posted August 7, 2007 Author Share Posted August 7, 2007 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.