簡體   English   中英

使用dom java解析xml

[英]parse xml using dom java

我有波紋管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。 但元素文檔存在於RankingDocslistOfDocs中 ,所以我有一個非常大的列表。

所以我想知道是否存在更好的解決方案來解析這個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方法。

您可以使用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++) {
   ...

如果您不想使用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"));
}

在這里查看GitHub項目。

希望這可以幫助。

我喜歡使用XMLBeam執行此類任務:

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());
        }
    }
}

甚至有一種方便的方法可以將百分比轉換為floatdouble 告訴我你是否想要一個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM