2016-07-26 31 views
0

Hier ist meine XMLVon XML einen untergeordneten Knoten in Java Erste

<?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> 

Hier ist meine JAVA-Code (Teil)

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

ich erfolgreich die Werte auf den NS3 zugeschrieben bekommen können: Location Knoten, aber kann nicht die Kind-Knoten Werte von "et" unter NS3: arr ...... dieser Teil meines Codes ist, wo ich sage // New Code. Ich bin neu in JAVA. Kann jemand mir helfen, den Wert von "et" unter NS3 zu erhalten: arr ... dh die Antwort ist 21.17. Danke

Antwort

1

seien Sie carrefull, das Sie versuchen, Daten von nlist zu erhalten, müssen Sie Kindergegenstand benutzen. dann ersetzen

Node cNode = nList.item(i); 

von

Node cNode = children.item(j); 

wie folgt:

 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

Großen Dank für die Antwort – user2635961