我有下面的xml:
<modelingOutput> <listOfTopics> <topic id="1"> <token id="354">wish</token> </topic> </listOfTopics> <rankedDocs> <topic id="1"> <documents> <document id="1" numWords="0"/> <document id="2" numWords="1"/> <document id="3" numWords="2"/> </documents> </topic> </rankedDocs> <listOfDocs> <documents> <document id="1"> <topic id="1" percentage="4.790644689978203%"/> <topic id="2" percentage="11.427632949428334%"/> <topic id="3" percentage="17.86913349249596%"/> </document> </documents> </listOfDocs> </modelingOutput>
Ι要解析此xml文件并从 ListofDocs 获取 主题ID 和 百分比 __
第一种方法是从xml获取所有文档元素,然后检查祖父节点是否为ListofDocs。但是元素文档存在于 rankDocs 和 listOfDocs中 ,因此我有一个很大的列表。
所以我想知道是否存在更好的解决方案来解析此xml以避免if语句?
我的代码:
public void parse(){ Document dom = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); dom = db.parse(is); Element doc = dom.getDocumentElement(); NodeList documentnl = doc.getElementsByTagName("document"); for (int i = 1; i <= documentnl.getLength(); i++) { Node item = documentnl.item(i); Node parentNode = item.getParentNode(); Node grandpNode = parentNode.getParentNode(); if(grandpNode.getNodeName() == "listOfDocs"{ //get value } } }
首先,在检查节点名称时,您不应String使用进行比较==。请始终使用该equals方法。
String
==
equals
您可以在以下位置使用XPath评估文档topic元素listOfDocs:
topic
listOfDocs
XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); XPathExpression xPathExpression = xPath.compile("//listOfDocs//document/topic"); NodeList topicnl = (NodeList) xPathExpression.evaluate(dom, XPathConstants.NODESET); for(int i = 0; i < topicnl.getLength(); i++) { ...