这是我的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:pass et="20:59" src="Darwin"/>
</ns3:Location>
</TS>
</uR>
</Pport>
以下是我的代码的一部分:
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();
}
我可以成功获取属性tpl,pta,ptd,wta和wtd。但是,我无法在节点名称空间 ns3:arr 中获取&#34; et&#34; 的属性。我不想得到&#34; et&#34;在 ns3:传递之内。我追求的答案是21.17。我骑自行车穿过ns3:Location节点。出现故障的代码片段在//新代码2下。我收到一个错误说明...方法getAttribute(String)未定义类型NodeList。任何帮助表示赞赏
答案 0 :(得分:0)
你几乎就在那里。 Element.getElementsByTagNameNS()
会返回NodeList
,而非Element
。因此,您必须以迭代Location
节点的方式迭代它:
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... */