2013-07-11 48 views
0
<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 XML中的特定標籤,我使用標籤<code code="43683-2"...>要使用JSOUP

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

現在,我怎麼能到第一<highlight>標籤<code code="43683-2"...>標籤後? 。 XML上有多個<highlight>標籤,我只想在特定代碼之後獲得第一個標籤。

因爲我在JSOUP或任何其他解析器上沒有以前的經驗,所以任何幫助都是非常有價值的。

問候。

回答

0

可以使用nextElementSibling方法Element類:

例:

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

這正是沒有解決我的問題,而是給了我一個辦法做到這一點。非常感謝。 – Sujan