[英]Getting A Child Node In Java From XML
Here is my XML 这是我的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>
Here is my JAVA code (part of) 这是我的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();
}
I can successfully get the values attributed to the NS3:Location node but cannot get the child node values of 'et' under NS3:arr......that part of my code is where I say //New Code. 我可以成功获取归因于NS3:Location节点的值,但无法获取NS3:arr下的'et'的子节点值……我的代码部分就是我说的//新代码。 I am new to JAVA . 我是JAVA的新手。 Can anybody help me get the value of 'et' under NS3:arr ...ie the answer is 21.17. 任何人都可以帮助我获得NS3:arr下的'et'值吗……即答案为21.17。 Thanks 谢谢
be carrefull you try to get data from nlist you must use children object. 如果不满意,您尝试从nlist获取数据,则必须使用children对象。 then replace 然后更换
Node cNode = nList.item(i);
by 通过
Node cNode = children.item(j);
as below: 如下:
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"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.