I am currently attempting to parse an XML file using JavaScript for a school assignment, and I am required to use pure JavaScript without jQuery. I am able to retrieve one value successfully, but encountering issues with another.
The problem lies within my JavaScript code responsible for extracting values from the XML file. Specifically, while I can successfully fetch the startLng value, the program returns startLat as undefined even though it is clearly defined in the XML file. I am unable to identify the root cause of this issue.
My objective is to extract both LatitudeDegrees and LongitudeDegrees from the XML file. However, the current implementation only retrieves LongitudeDegrees successfully, leaving LatitudeDegrees as undefined. What mistake could I possibly be making?
function loadXMLDoc() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
getLatLang(xmlhttp);
}
};
xmlhttp.open("GET", "G1.TCX", true);
xmlhttp.send();
}
function getLatLang(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("Trackpoint");
startLat = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LatitudeDegrees")[0].childNodes[0].nodeValue;
startLng = x[0].getElementsByTagName("Position")[0].getElementsByTagName("LongitudeDegrees")[0].childNodes[0].nodeValue;
}
Below is a snippet from the XML file:
<Trackpoint>
<Time>2008-10-28T15:58:22Z</Time>
<Position>
<LatitudeDegrees>59.4111992</LatitudeDegrees>
<LongitudeDegrees>13.5304104</LongitudeDegrees>
</Position>
<AltitudeMeters>85.6945801</AltitudeMeters>
<DistanceMeters>0.2149343</DistanceMeters>
<HeartRateBpm xsi:type="HeartRateInBeatsPerMinute_t">
<Value>116</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
<Extensions>
<TPX xmlns="http://www.garmin.com/xmlschemas/ActivityExtension/v2" CadenceSensor="Footpod"/>
</Extensions>
</Trackpoint>