我的XML文件如下:
<Data>
<goals1>
<OwnerId>51</OwnerId>
<PassionId>305</PassionId>
<GoalId>232</GoalId>
<goalname>sdjhjhd</goalname>
</goals1>
<goals1>
<OwnerId>51</OwnerId>
<PassionId>291,305</PassionId>
<GoalId>231</GoalId>
<goalname>hfhjkjk</goalname>
</goals1>
<goals1>
<OwnerId>51</OwnerId>
<PassionId>308</PassionId>
<GoalId></GoalId>
<goalname>hfhjkjk</goalname>
</goals1>
<Data>
想要检查特定激情ID下是否存在目标ID。
答案 0 :(得分:0)
DocumentBuilder db=Null;
Document doc = null;
在XML Read函数中:
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException ex) {
//Logger.getLogger(JavaTestApps.class.getName()).log(Level.SEVERE, null, ex);
}
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(yourxmlstring));
try {
doc = db.parse(is);
} catch (SAXException ex) {
//Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
// Logger.getLogger(this.class.getName()).log(Level.SEVERE, null, ex);
}
NodeList nodes = doc.getElementsByTagName("mainTagofXML");
// System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
System.out.println("Hello : "+element);
NodeList name = element.getElementsByTagName("yourItem");
Element line = (Element) name.item(0);
--- check element is exsists
}
希望这个帮助