2015-06-18 146 views
0

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

雖然這是給我一個空指針異常

回答

1

像這樣的事情

public static void main(String[] args) throws MalformedURLException, SAXException, IOException{ 

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 

// Get the DOM Builder 
DocumentBuilder docBuilder; 
try { 
    docBuilder = factory.newDocumentBuilder(); 
    String dstation ="Dublin%20Pearse"; 

    URL url = new URL("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByNameXML?StationDesc=" + dstation); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestMethod("GET"); 
    connection.setDoOutput(true); 
    connection.connect(); 
    Document doc = (Document) docBuilder.parse(connection.getInputStream()); 
    doc.getDocumentElement().normalize(); 
    NodeList trains = doc.getElementsByTagName("objStationData"); 
    NodeList nodes = trains.item(0).getChildNodes(); 

    for(int i = 0; i<nodes.getLength(); i++){ 
     if(nodes.item(i).getNodeName().equals("Direction")){ // what should go here 
      // do something 
     } 

    } 
} catch (ParserConfigurationException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

} 
0

。我給你一個簡單的例子,你如何讀取XML中的所有信息。

http://www.mkyong.com/java/jaxb-hello-world-example/

+0

是不是在那裏沒有Marshaller就可以做到。看我的編輯。 –

+0

我想我會問這個錯誤作爲一個單獨的問題 –

0

如果您使用的是Java 8,你可以嘗試使用XmlDynamic

XmlDynamic stations = new XmlDynamic(
    "<ArrayOfObjStationData>" + 
     "<objStationData>" + 
      "<Direction>Northbound</Direction>" + 
     "</objStationData>" + 
     "<objStationData>" + 
      "<Direction>Southbound</Direction>" + 
     "</objStationData>" + 
    "</ArrayOfObjStationData>"); 

stations.get("ArrayOfObjStationData").children() 
    .filter(XmlDynamic.hasElementName("objStationData")) 
    .forEach(stationData -> { 
     // doSomething() 

     // print out all Direction children text, Northbound & Southbound 
     System.out.println(stationData.get("Direction").asString()); 
    }); 

見​​