簡體   English   中英

使用JSOUP到XML中的特定標簽

[英]To the certain tag in XML using JSOUP

<code code="43683-2" codeSystem="2.16.840.1.113883.6.1" displayName="RECENT MAJOR CHANGES SECTION"/>
               <title/>
               <effectiveTime value="20111004"/>
               <excerpt>
                  <highlight>
                     <text>
                        <paragraph>Contraindications <linkHtml href="#s4">(4)</linkHtml>   10/2011</paragraph>
                        <paragraph>Warnings and Precautions, Use in Pregnant Women with Mechanical Heart Valves <linkHtml href="#s5.5">(5.5)</linkHtml>   10/2011</paragraph>
                     </text>
                  </highlight>
               </excerpt>

在此XML中,我使用標記<code code="43683-2"...>

for (Element e : doc.select("code")) {
            if (e.attr("code").trim().equals("43683-2")){
            //codes
            }
}

現在,如何到達<code code="43683-2"...>標記之后的第一個 <highlight> <code code="43683-2"...>標記? XML上有多個<highlight>標記,我只想獲取該特定代碼之后的第一個標記。

由於我以前沒有JSOUP或任何其他解析器的經驗,因此任何幫助都是非常寶貴的。

問候。

您可以使用Element類的nextElementSibling方法:

例如:

for (Element e : doc.select("code")) {
    if (e.attr("code").trim().equals("43683-2")) {
        Element firstHighlight = null;     
        Element sibling = e.nextElementSibling();
        while (sibling != null && firstHighlight == null) {
            if (sibling.tagName().equals("highlight")) {
                firstHighlight = sibling;
            } else {
                sibling = sibling.nextElementSibling();
            }
        }
    }
}

暫無
暫無

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

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