XML:獲取XML信息的Java
<ArrayOfObjStationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.irishrail.ie/realtime/">
<objStationData>
<Servertime>2015-06-18T12:46:40.02</Servertime>
<Traincode>E813</Traincode>
<Stationfullname>Dublin Pearse</Stationfullname>
<Stationcode>PERSE</Stationcode>
<Querytime>12:46:40</Querytime>
<Traindate>18 Jun 2015</Traindate>
<Origin>Greystones</Origin>
<Destination>Malahide</Destination>
<Origintime>12:00</Origintime>
<Destinationtime>13:20</Destinationtime>
<Status>En Route</Status>
<Lastlocation>Departed Sandymount</Lastlocation>
<Duein>7</Duein>
<Late>4</Late>
<Exparrival>12:52</Exparrival>
<Expdepart>12:53</Expdepart>
<Scharrival>12:48</Scharrival>
<Schdepart>12:49</Schdepart>
<Direction>Northbound</Direction>
<Traintype>DART</Traintype>
<Locationtype>S</Locationtype>
</objStationData>
<objStationData>
...
</objStationData>
</ArrayOfObjStationData>
代碼:
Document doc = (Document)docBuilder.parse(new URL("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByNameXML?StationDesc=" + dstation).openStream());
doc.getDocumentElement().normalize();
NodeList trains = doc.getElementsByTagName("objStationData");
for (int i = 0; i < trains.getLength(); i++)
{
if (trains.item(i).tagName.equals("Direction"){ // what should go here
doSomething();
}
}
編輯:
我覺得這件事情是這樣的:
012如果你想利用信息從XML在Java中,你可以使用的Marshaller雖然這是給我一個空指針異常
是不是在那裏沒有Marshaller就可以做到。看我的編輯。 –
我想我會問這個錯誤作爲一個單獨的問題 –