2016-07-28 28 views
0

Hier mein XMLParsen von XML mit Java Anprobieren Namespace childNode Attribut Get

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

Hier ist ein Teil meines Code:

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

Ich kann erfolgreich Attribute tpl bekommen, pta, PTD, wta und wtd. Jedoch kann ich das Attribut für "et" innerhalb des Knotennamespace ns3 nicht erhalten: arr. Ich möchte nicht das "et" innerhalb ns3 erhalten: pass. Die Antwort ist 21.17. Ich fahre durch den Knoten ns3: Location. Der Code, der den Fehler verursacht, befindet sich unter // Neuer Code 2. Ich erhalte eine Fehlermeldung, dass ... Die Methode getAttribute (String) ist für den Typ NodeList nicht definiert. Jede Hilfe geschätzt

+1

'getElementsByTagNameNS' gibt einen' 'NodeList. Wahrscheinlich möchten Sie 'item()' darauf aufrufen, um ein bestimmtes Element zu erhalten, das Sie 'getAttribute()' aufrufen können. – GriffeyDog

+0

Hallo GriffeyDog. Meinst du das? System.out.println (eElement.getElementsByTagNameNS ("http .......", "arr"). Item (0) .getAttribute ("et")); .. ............. es wird immer noch als Fehler angezeigt – user2635961

Antwort

0

Sie sind fast da. Element.getElementsByTagNameNS() gibt eine NodeList, keine Element zurück. Also muss man über sie iterieren die gleiche Art und Weise Sie Ihre iterieren Location Knoten:

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... */