2016-07-28 59 views
0

這裏是我的XML解析XML在Java試圖獲得屬性名稱空間Childnode

<?xml version="1.0" encoding="UTF-8"?> 
    <Pport xmlns="http://www.thalesgroup.com/rtti/PushPort/v12" xmlns:ns3="http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2" ts="2016-07-26T20:51:30.9941484+01:00" version="12.0"> 
    <uR updateOrigin="TD"> 
    <TS rid="201607264120116" ssd="2016-07-26" uid="W44875"> 
    <ns3:Location tpl="LEEE" wtp="21:00:30"> 
     <ns3:pass et="20:57" src="TD"/> 
     <ns3:plat cisPlatsup="true" platsrc="A" platsup="true">1</ns3:plat> 
    </ns3:Location> 
    <ns3:Location tpl="LEESPRJ" wtp="21:02:30"> 
    <ns3:pass et="20:59" src="Darwin"/> 
    </ns3:Location> 
    <ns3:Location tpl="GRVPDCE" wtp="21:15:30"> 
    <ns3:pass et="21:12" src="Darwin"/> 
    </ns3:Location> 
    <ns3:Location tpl="GRVPCSD" wta="21:21"> 
    <ns3:arr et="21:17" src="Darwin"/> 
    <ns3:pass et="20:59" src="Darwin"/> 
    </ns3:Location> 
    </TS> 
    </uR> 
</Pport> 

這裏是我的代碼部分:

try { 
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    DocumentBuilder builder = factory.newDocumentBuilder(); 
    InputSource is = new InputSource(new StringReader(xml)); 
    Document doc = builder.parse(is); 

    doc.getDocumentElement().normalize(); 
    System.out.println("Root Element : " + doc.getDocumentElement().getNodeName()); 

    NodeList nList = doc.getElementsByTagNameNS(
     "http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2", "Location"); 
    int totalBooks = nList.getLength(); 
    System.out.println(totalBooks); 

    for (int i = 0; i < nList.getLength(); i++) { 
     Node nNode = nList.item(i); 
     if (nNode.getNodeType() == Node.ELEMENT_NODE) { 
      Element eElement = (Element) nNode; 
      System.out.println(eElement.getAttribute("tpl")); 

      String tpl = eElement.getAttribute("tpl"); 
      String pta = eElement.getAttribute("pta"); 
      String ptd = eElement.getAttribute("ptd"); 
      String wta = eElement.getAttribute("wta"); 
      String wtd = eElement.getAttribute("wtd"); 

      //New Code 2 
      System.out.println(eElement.getElementsByTagNameNS(
       "http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2","arr").getAttribute("et")); 

      String query = "insert into darwinall (tpl,timestamp,pta,ptd,wta,wtd)" 
      + "values(?,?,?,?,?,?)"; 
      try { 
       PreparedStatement preparedStmt = connectOut.connect().prepareStatement(query); 
       preparedStmt.setString(1, tpl); 
       preparedStmt.setInt(2, 123456); 
       preparedStmt.setString(3, pta); 
       preparedStmt.setString(4, ptd); 
       preparedStmt.setString(5, wta); 
       preparedStmt.setString(6, wtd); 

       preparedStmt.execute(); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} 

我能順利拿到屬性TPL,PTA,工信部, wta和wtd。但是我無法獲得節點名稱空間ns3:arr內的「et」的屬性。我不想在ns3:pass內獲得「et」。我以後的答案是21.17。我正在通過ns3:Location節點騎車。那段錯誤代碼是//新代碼2.我得到一個錯誤,說...方法getAttribute(String)對於NodeList類型是未定義的。任何幫助讚賞

+1

'getElementsByTagNameNS'返回'NodeList'。你可能想調用'item()'來獲得一個你可以調用'getAttribute()'的特定項。 – GriffeyDog

+0

嗨GriffeyDog。你的意思是這樣... System.out.println(eElement.getElementsByTagNameNS(「http ........」,「arr」)。item(0).getAttribute(「et」)); .. .............它仍然顯示爲錯誤 – user2635961

回答

0

你幾乎在那裏。 Element.getElementsByTagNameNS()返回NodeList,而不是Element。因此,您必須按照迭代Location節點的方式迭代它:

String tpl = eElement.getAttribute("tpl"); 
String pta = eElement.getAttribute("pta"); 
String ptd = eElement.getAttribute("ptd"); 
String wta = eElement.getAttribute("wta"); 
String wtd = eElement.getAttribute("wtd"); 
String et = ""; 

NodeList nArrList = eElement.getElementsByTagNameNS(
     "http://www.thalesgroup.com/rtti/PushPort/Forecasts/v2", "arr"); 

for (int k = 0; k < nArrList.getLength(); k++) { 
    Node innerNode = nArrList.item(k); 
    if (innerNode.getNodeType() == Node.ELEMENT_NODE) { 
     Element innerElt = (Element) innerNode; 
     et = innerElt.getAttribute("et"); 
    } 
} 

/* Do database stuff... */