使用domjava解析xml
我有下面的xml:使用domjava解析xml,java,xml,dom,Java,Xml,Dom,我有下面的xml: <modelingOutput> <listOfTopics> <topic id="1"> <token id="354">wish</token> </topic> </listOfTopics> <rankedDocs> <topic id="1">
<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文件,并从列表中获取主题id和百分比
第一种方法是从xml获取所有文档元素,然后检查祖父节点是否为ListofDocs。
但是元素文档存在于rankedDocs和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
}
}
}
public void parse(){
文档dom=null;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
InputSource is=新的InputSource(新的StringReader(xml));
dom=db.parse(is);
Element doc=dom.getDocumentElement();
NodeList documentnl=doc.getElementsByTagName(“文档”);
对于(int i=1;i首先,在检查节点名称时,不应使用=
比较字符串
s。请始终使用等于
方法
您可以使用XPath仅计算listOfDocs
下的文档topic
元素:
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++) {
...
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
XPathExpression=xPath.compile(“//listOfDocs//document/topic”);
NodeList topicnl=(NodeList)xPathExpression.evaluate(dom,XPathConstants.NODESET);
对于(int i=0;i
如果不想使用If语句,可以使用XPath直接获取所需的元素
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("source.xml");
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("/*/listOfDocs/documents/document/topic");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getAttributes().getNamedItem("id"));
System.out.println(nodes.item(i).getAttributes().getNamedItem("percentage"));
}
DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(“source.xml”);
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathfactory.newXPath();
XPathExpression expr=xpath.compile(“/*/listOfDocs/documents/document/topic”);
NodeList节点=(NodeList)expr.evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i
请检查GitHub项目
希望这能有所帮助。我喜欢用它来完成这样的任务:
public class Answer {
@XBDocURL("resource://data.xml")
public interface DataProjection {
public interface Topic {
@XBRead("./@id")
int getID();
@XBRead("./@percentage")
String getPercentage();
}
@XBRead("/modelingOutput/listOfDocs//document/topic")
List<Topic> getTopics();
}
public static void main(final String[] args) throws IOException {
final DataProjection dataProjection = new XBProjector().io().fromURLAnnotation(DataProjection.class);
for (Topic topic : dataProjection.getTopics()) {
System.out.println(topic.getID() + ": " + topic.getPercentage());
}
}
}
公共类答案{
@XBDocURL(“resource://data.xml")
公共接口数据投影{
公共接口主题{
@XBRead(“./@id”)
int getID();
@XBRead(“百分比”)
字符串getPercentage();
}
@XBRead(“/modelingOutput/listOfDocs//document/topic”)
列出getTopics();
}
公共静态void main(最终字符串[]args)引发IOException{
final DataProjection DataProjection=new XBProjector().io().fromURLAnnotation(DataProjection.class);
for(主题:dataProjection.getTopics()){
System.out.println(topic.getID()+”:“+topic.getPercentage());
}
}
}
甚至还有一种方便的方法可以将百分比转换为浮点值
或双精度
。如果你想举个例子,请告诉我。它恰好与manouti提出的解决方案基本相同,只是稍微详细一点。可能同时正在处理它。我将把它留在这里以备参考,以防万一你想看看。