Problemer med XML i JS
HejsaJeg laver et script hvor noget xml skal bearbejdes.
Jeg loader xml'en og laver domobject sådan:
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(inputXml, "text/xml");
}
else // Internet Explorer
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(inputXml);
}
Først hiver jeg køretøjer ud med:
var oXMLVehicleNodeList = xmlDoc.selectNodes("/ArrayOfvhc_vehicle/vhc_vehicle");
Dette virker fint. Men når jeg så forsøger at få crd_coodinates ud vil den ikke give mig noget tilbage:
var oXMLCoordinatesNodeList = xmlDoc.selectNodes("/vhc_vehicle/crd_coordinate/crd_coordinate");
alert(oXMLCoordinatesNodeList.length);
Jeg har tjekket det i xmlspy, og den giver mig det jeg ønsker. Min alert test returnere 0.
Min XML ser således ud:
<ArrayOfvhc_vehicle xmlns="http://schemas.datacontract.org/2004/07/VehicleInformationDB" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<vhc_vehicle z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<EntityKey z:Id="i2" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>vhc_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">1</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>vhc_vehicle</a:EntitySetName>
</EntityKey>
<crd_coordinate>
<crd_coordinate z:Id="i3">
<EntityKey z:Id="i4" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>crd_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">3</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>crd_coordinate</a:EntitySetName>
</EntityKey>
<crd_id>3</crd_id>
<crd_latitude>55.34078</crd_latitude>
<crd_longitude>10.0352 </crd_longitude>
<crd_received>2009-12-01T01:00:00</crd_received>
<evl_eventlog/>
<snr_sensorreading/>
<sta_state i:nil="true"/>
<sta_stateReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i5" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>sta_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>sta_state</b:EntitySetName>
</a:EntityKey>
</sta_stateReference>
<trp_trip i:nil="true"/>
<trp_tripReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i6" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>trp_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>trp_trip</b:EntitySetName>
</a:EntityKey>
</trp_tripReference>
<vhc_vehicle z:Ref="i1"/>
<vhc_vehicleReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"/>
</vhc_vehicleReference>
</crd_coordinate>
<crd_coordinate z:Id="i7">
<EntityKey z:Id="i8" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>crd_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">4</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>crd_coordinate</a:EntitySetName>
</EntityKey>
<crd_id>4</crd_id>
<crd_latitude>55.34035</crd_latitude>
<crd_longitude>10.03275</crd_longitude>
<crd_received>2009-12-01T02:00:00</crd_received>
<evl_eventlog/>
<snr_sensorreading/>
<sta_state i:nil="true"/>
<sta_stateReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i9" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>sta_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>sta_state</b:EntitySetName>
</a:EntityKey>
</sta_stateReference>
<trp_trip i:nil="true"/>
<trp_tripReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i10" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>trp_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>trp_trip</b:EntitySetName>
</a:EntityKey>
</trp_tripReference>
<vhc_vehicle z:Ref="i1"/>
<vhc_vehicleReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"/>
</vhc_vehicleReference>
</crd_coordinate>
<crd_coordinate z:Id="i11">
<EntityKey z:Id="i12" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>crd_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">5</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>crd_coordinate</a:EntitySetName>
</EntityKey>
<crd_id>5</crd_id>
<crd_latitude>55.34078</crd_latitude>
<crd_longitude>10.03782</crd_longitude>
<crd_received>2009-12-01T03:00:00</crd_received>
<evl_eventlog/>
<snr_sensorreading/>
<sta_state i:nil="true"/>
<sta_stateReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i13" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>sta_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>sta_state</b:EntitySetName>
</a:EntityKey>
</sta_stateReference>
<trp_trip i:nil="true"/>
<trp_tripReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Id="i14" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data">
<b:EntityContainerName>FleetViewEntities</b:EntityContainerName>
<b:EntityKeyValues>
<b:EntityKeyMember>
<b:Key>trp_id</b:Key>
<b:Value i:type="c:int" xmlns:c="http://www.w3.org/2001/XMLSchema">1</b:Value>
</b:EntityKeyMember>
</b:EntityKeyValues>
<b:EntitySetName>trp_trip</b:EntitySetName>
</a:EntityKey>
</trp_tripReference>
<vhc_vehicle z:Ref="i1"/>
<vhc_vehicleReference xmlns:a="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<a:EntityKey z:Ref="i2" xmlns:b="http://schemas.datacontract.org/2004/07/System.Data"/>
</vhc_vehicleReference>
</crd_coordinate>
</crd_coordinate>
<evl_eventlog/>
<snr_sensorreading/>
<sns_sensor/>
<trp_trip/>
<vhc_id>1</vhc_id>
<vhc_number>123</vhc_number>
<vhg_vhc_connection/>
</vhc_vehicle>
<vhc_vehicle z:Id="i2339" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<EntityKey z:Id="i2340" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>vhc_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">2</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>vhc_vehicle</a:EntitySetName>
</EntityKey>
<crd_coordinate/>
<evl_eventlog/>
<snr_sensorreading/>
<sns_sensor/>
<trp_trip/>
<vhc_id>2</vhc_id>
<vhc_number>124</vhc_number>
<vhg_vhc_connection/>
</vhc_vehicle>
<vhc_vehicle z:Id="i2341" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<EntityKey z:Id="i2342" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses" xmlns:a="http://schemas.datacontract.org/2004/07/System.Data">
<a:EntityContainerName>FleetViewEntities</a:EntityContainerName>
<a:EntityKeyValues>
<a:EntityKeyMember>
<a:Key>vhc_id</a:Key>
<a:Value i:type="b:int" xmlns:b="http://www.w3.org/2001/XMLSchema">3</a:Value>
</a:EntityKeyMember>
</a:EntityKeyValues>
<a:EntitySetName>vhc_vehicle</a:EntitySetName>
</EntityKey>
<crd_coordinate/>
<evl_eventlog/>
<snr_sensorreading/>
<sns_sensor/>
<trp_trip/>
<vhc_id>3</vhc_id>
<vhc_number>125</vhc_number>
<vhg_vhc_connection/>
</vhc_vehicle>
</ArrayOfvhc_vehicle>