久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Xpath 檢索 XML 樹的節(jié)點(diǎn)后的節(jié)點(diǎn)?

How to retrieve node after node of XML tree using Xpath?(如何使用 Xpath 檢索 XML 樹的節(jié)點(diǎn)后的節(jié)點(diǎn)?)
本文介紹了如何使用 Xpath 檢索 XML 樹的節(jié)點(diǎn)后的節(jié)點(diǎn)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

首先,我必須說(shuō),我發(fā)現(xiàn) Xpath 是一個(gè)非常好的解析器,并且在與其他解析器進(jìn)行比較時(shí),我認(rèn)為它非常強(qiáng)大.

First, I must say that I find Xpath as a very nice parser , and I guess pretty powerful when comparing it to other parsers .

給定以下代碼:

  DocumentBuilderFactory domFactory = 
  DocumentBuilderFactory.newInstance();
  domFactory.setNamespaceAware(true); 
  DocumentBuilder builder = domFactory.newDocumentBuilder();
  Document doc = builder.parse("input.xml");
  XPath xpath = XPathFactory.newInstance().newXPath();

如果我想找到第 1 輪的 first 節(jié)點(diǎn) &1號(hào)門,這里:

If I wanted to find the first node of Round 1 & Door 1 , here :

<Game>
    <Round>
        <roundNumber>1</roundNumber>
        <Door>
            <doorName>abd11</doorName>
            <Value>
                <xVal1>0</xVal1>
                <xVal2>25</xVal2>
                <pVal>0.31</pVal>
            </Value>
            <Value>
                <xVal1>25</xVal1>
                <xVal2>50</xVal2>
                <pVal>0.04</pVal>
            </Value>
            <Value>
                <xVal1>50</xVal1>
                <xVal2>75</xVal2>
                <pVal>0.19</pVal>
            </Value>
            <Value>
                <xVal1>75</xVal1>
                <xVal2>100</xVal2>
                <pVal>0.46</pVal>
            </Value>
        </Door>
        <Door>
            <doorName>vvv1133</doorName>
            <Value>
                <xVal1>60</xVal1>
                <xVal2>62</xVal2>
                <pVal>1.0</pVal>
            </Value>
        </Door>
    </Round>
    <Round>
        <roundNumber>2</roundNumber>
        <Door>
            <doorName>eee</doorName>
            <Value>
                <xVal1>0</xVal1>
                <xVal2>-25</xVal2>
                <pVal>0.31</pVal>
            </Value>
            <Value>
                <xVal1>-25</xVal1>
                <xVal2>-50</xVal2>
                <pVal>0.04</pVal>
            </Value>
            <Value>
                <xVal1>-50</xVal1>
                <xVal2>-75</xVal2>
                <pVal>0.19</pVal>
            </Value>
            <Value>
                <xVal1>-75</xVal1>
                <xVal2>-100</xVal2>
                <pVal>0.46</pVal>
            </Value>
        </Door>
        <Door>
            <doorName>cc</doorName>
            <Value>
                <xVal1>-60</xVal1>
                <xVal2>-62</xVal2>
                <pVal>0.3</pVal>
            </Value>
            <Value>
                <xVal1>-70</xVal1>
                <xVal2>-78</xVal2>
                <pVal>0.7</pVal>
            </Value>
        </Door>
    </Round>
</Game>

我會(huì)這樣做的:

 XPathExpression expr = xpath.compile("http://Round[1]/Door[1]/Value[1]/*/text()");      
  Object result = expr.evaluate(doc, XPathConstants.NODESET);
  NodeList nodes = (NodeList) result;

如果我想要第 1 輪的 second 節(jié)點(diǎn) &那么門1:

and if I wanted the second node of Round 1 & Door 1 then :

XPathExpression expr = xpath.compile("http://Round[1]/Door[1]/Value[2]/*/text()");  

但是我如何使用循環(huán)來(lái)做到這一點(diǎn),因?yàn)槲也恢牢矣卸嗌?Value-nodes ,這意味著我如何使用循環(huán)來(lái)做到這一點(diǎn),每次迭代我都會(huì)檢索 3(我的意思是 xVal1xVal2pVal 值)更多值節(jié)點(diǎn)的值!?

but how do I do this using a loop , since I don't know how much Value-nodes I have , meaning how can I do this using a loop , where each iteration I retrieve 3 (I mean the xVal1 , xVal2 and pVal values ) more values of a Value node !?

要求這樣做的原因是:

  1. 我不知道我有多少Round-s

我不知道我有多少Value-s

我不想每次都聲明一個(gè)新的XPathExpression

I don't want to declare every time a new XPathExpression

謝謝.

推薦答案

選項(xiàng) 1 - 遍歷文檔中的所有 Value 元素.只需要一次評(píng)估,但很難知道該值屬于哪個(gè)圓形或門元素.

Option 1 - Iterate over all Value elements in the document. Only one evaluation required, but difficult to know which Round or Door element the Value belongs to.

NodeList result = (NodeList) xpath.evaluate("http://Round/Door/Value/*/text()", doc, XPathConstants.NODESET);

選項(xiàng) 2 - 分別迭代每個(gè) Round、Door 和 Value 元素.需要更多評(píng)估,但上下文很容易知道.如果需要索引,很容易在循環(huán)中添加一個(gè)計(jì)數(shù)器.

Option 2 - Iterate over each Round, Door and Value elements separately. Requires more evaluations but the context is easily known. If index is required, it is easy to add a counter to the loops.

// Get all rounds and iterate over them
NodeList rounds = (NodeList) xpath.evaluate("http://Round", doc, XPathConstants.NODESET);
for (Node round : rounds) {
  // Get all doors and iterate over them
  NodeList doors = (NodeList) xpath.evaluate("Door", round, XPathConstants.NODESET);
  for (Node door : doors) {
    // Get all values and iterate over them
    NodeList values = (NodeList) xpath.evaluate("Value/*/text()", door, XPathConstants.NODESET);
    for (Node value : values) {
      // Do something
    }
  }
}

選項(xiàng) 3 - 根據(jù)您的要求將上述方法組合起來(lái)

Option 3 - Do some combination of the above depending on your requirements

請(qǐng)注意,我刪除了表達(dá)式編譯步驟以縮短示例.應(yīng)該重新添加它以提高性能.

Note that I've removed the expression compilation step to shorten the example. It should be re-added to improve performance.

這篇關(guān)于如何使用 Xpath 檢索 XML 樹的節(jié)點(diǎn)后的節(jié)點(diǎn)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽(tīng)器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無(wú)效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 亚洲免费人成在线视频观看 | 麻豆av在线 | 国产一区二区三区在线免费观看 | 欧美精品一区二区三区蜜桃视频 | 国产一二三区精品视频 | 中文字幕在线一区二区三区 | 久久99精品久久久97夜夜嗨 | 国产精品亚洲第一区在线暖暖韩国 | 日韩精品免费在线观看 | 韩日精品一区 | 久久精品97 | 91成人免费 | 色在线视频网站 | 三级视频国产 | 一级片子 | 成人av高清 | 成人精品一区二区三区中文字幕 | av网站在线播放 | 午夜视频在线观看网站 | 久久一区二区三区四区五区 | 国产福利91精品一区二区三区 | 久久高清| 国产二区视频 | 九九在线视频 | 性视频网| 亚洲一区二区三区四区五区午夜 | 午夜电影福利 | 国精日本亚洲欧州国产中文久久 | 国产福利91精品一区二区三区 | 欧美午夜视频 | 久草新在线 | 国产精品久久久久久久久久 | 久久久人成影片一区二区三区 | 久久97精品| 成人免费观看视频 | 国产一区二区在线免费观看 | 91亚洲国产成人精品一区二三 | 国产一区二区三区在线视频 | 91天堂网| 欧美在线视频免费 | 美女黄视频网站 |