2016-07-26 124 views
0

這裏是我的XML獲取子節點在Java從XML

<?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:Location> 
    </TS> 
    </uR> 
</Pport> 

這裏是我的JAVA代碼(部分)

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); 

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


        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"); 

          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(); 
          } 

         } 

         // New Code 
         NodeList children = nList.item(i).getChildNodes(); 
         for (int j = 0; j < children.getLength(); j++) { 
          Node cNode = nList.item(i); 
          if (cNode.getNodeType() == Node.ELEMENT_NODE) { 
           Element eElement = (Element) cNode; 
           System.out.println(eElement.getAttribute("et")); 
          } 
         } 


        } 
       } catch (Exception ex) { 
        ex.printStackTrace(); 
       } 

我能順利拿到價值歸因於NS3:位置節點,但不能得到'et'的子節點值在NS3:arr下......我的代碼的一部分就是我說//新代碼的地方。我是JAVA新手。任何人都可以幫助我獲得NS3下的'et'值:即答案是21.17。謝謝

回答

1

被carrefull你試圖從nlist中獲取數據你必須使用children對象。 然後通過

Node cNode = children.item(j); 
如下

更換

Node cNode = nList.item(i); 

 NodeList children = nList.item(i).getChildNodes(); 
     for (int j = 0; j < children.getLength(); j++) { 

      Node cNode = children.item(j); 
      if (cNode.getNodeType() == Node.ELEMENT_NODE) { 
       Element eElement = (Element) cNode; 
       System.out.println(eElement.getAttribute("et")); 
      } 
     } 
+0

的答案十分感謝 – user2635961