簡體   English   中英

使用xpath讀取多個XML屬性

[英]Reading multiple XML attributes with xpath

所以說我有這樣的事情:

<info>
    <collectorKey id="key"/>
    <credentials user="user" password="password"/>
    <Infos>
        <dummyInfo>
            <infoSource temp="N/A">               
                <tags>
                    <tag id="1" value="example"/>
                    <tag id="2" value="example"/>
                    <tag id="3" value="example"/>
                    <tag id="4" value="example"/>
                    <tag id="5" value="example"/>
                    <tag id="6" value="example"/>
                    <tag id="7" value="example"/>
                </tags>
            </infoSource>
        </dummyInfo>
    </Infos>
    <Infos>
        <dummyInfo>
            <infoSource temp="N/A">               
                <tags>
                    <tag id="1" value="example"/>
                    <tag id="2" value="example"/>
                    <tag id="3" value="example"/>
                    <tag id="4" value="example"/>
                    <tag id="5" value="example"/>
                    <tag id="6" value="example"/>
                    <tag id="7" value="example"/>
                </tags>
            </infoSource>
        </dummyInfo>
    </Infos>
</info>

我想獲取ID為2的每個標簽屬性,並獲取該標簽的值。 現在,我有一些冗長的代碼可以執行此操作,而且它似乎不太實用。 我想盡可能將其轉換為使用xpath,我想知道如何實現。 我把一些框架代碼放在一起,但它並沒有將我的標簽值與2捆綁在一起。我認為需要添加一些循環以及一些更改。

就像是:

try {
        ClassLoader classLoader = getClass().getClassLoader();
        File configProdXML = new File(classLoader.getResource("files/config-prod.xml").getFile());

        //parse it using a docbuilder
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = null;
        dBuilder = dbFactory.newDocumentBuilder();
        Document parsedConfig = dBuilder.parse(configProdXML);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("/tags/tags/@id=2/text()");

        System.out.println(expr.toString());

邊注。 為此,xpath會是最實用的東西嗎?

是的,XPath非常適合:

XPathExpression expr = xpath.compile("//tags/tag[@id=2]/@example");

一些解釋:

  • //tags的兩個斜杠表示文檔中任何位置,任何級別的所有<tags>元素。
  • tag[@id=2]表示<tag>元素,但帶有限制哪些元素符合條件的謂詞。

通常,除非計划將相同的XPath應用於許多不同的源,否則不必直接調用XPath.compile。 您可以直接調用evaluate方法:

NodeList values = (NodeList)
    xpath.evaluate("//tags/tag[@id=2]/@example", parsedConfig,
        XPathConstants.NODESET);

注意:切勿調用URL.getFile()。 它不會將URL轉換為有效的文件名-只是返回URL在主機和端口之后的部分,其中可能包含許多不允許在URL中出現的字符的轉義符。 同樣,也不保證Class.getResource或ClassLoader.getResource返回的URL完全指向文件。 特別是,如果您嘗試從.jar文件運行,則不會獲得file: URL。

幸運的是,您不需要文件。 您甚至不需要解析文檔。 您可以將InputStream直接傳遞到XPath:

try (InputStream config = getClass().getResourceAsStream("/files/config-prod.xml")) {

    NodeList values = (NodeList)
        xpath.evaluate("//tags/tag[@id=2]/@example", new InputSource(config),
            XPathConstants.NODESET);

    int count = values.getLength();
    for (int i = 0; i < count; i++) {
        String value = values.item(i).getNodeValue();
        System.out.println("Found value \"" + value + "\"");
    }
}

暫無
暫無

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

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