使用JSOUP到XML中的某个标记

时间:2013-07-11 09:52:18

标签: java xml xml-parsing 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
            }
}

现在,如何在<highlight>标记后找到第一个 <code code="43683-2"...>标记? XML上有多个<highlight>标记,我只想在该特定代码之后获取第一个标记。

由于我以前没有JSOUP或任何其他解析器的经验,所以任何帮助都非常有价值。

问候。

1 个答案:

答案 0 :(得分: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();
            }
        }
    }
}