这是我的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:Location节点的值,但无法在NS3下获取'et'的子节点值:arr ......我的代码部分是我说的//新代码。我是JAVA的新手。任何人都可以帮我在NS3下获得'et'的价值:arr ...即答案是21.17。谢谢
答案 0 :(得分:1)
如果您尝试从nlist获取数据,则必须使用子对象。 然后替换
Node cNode = nList.item(i);
通过
Node cNode = children.item(j);
如下:
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"));
}
}