java - 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 , precautions, use in pregnant women mechanical heart valves <linkhtml href="#s5.5">(5.5)</linkhtml>   10/2011</paragraph>                      </text>                   </highlight>                </excerpt> 

in xml, got tag <code code="43683-2"...> using

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

now, how can first <highlight> tag after <code code="43683-2"...> tag?. there multiple <highlight> tags on xml , want first 1 after specific code.

since have no previous experience on jsoup or other parsers, extremely valuable.

regards.

you can use nextelementsibling method of element class:

ex:

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

Comments