首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSoup查询:如何在另一个标记中查找标记的所有实例

JSoup查询:如何在另一个标记中查找标记的所有实例
EN

Stack Overflow用户
提问于 2016-06-07 14:26:05
回答 1查看 291关注 0票数 0

我有一个HTML页面来扫描标题和图形元素。HTML文件的代码是:

代码语言:javascript
复制
<body>
<section>
    <h2>H2-1</h2>
    <div>
        <section>
            <h3>H3-1</h3>
            <div>
                <figure><img src="Fig 1.png"></figure>
            </div>
        </section>
        <section>
            <h3>H3-2</h3>
            <div></div>
        </section>
    </div>
</section>
<section>
    <h2>H2-2</h2>
    <div>
        <figure><img src="Fig 2.png"></figure>
        <figure><img src="Fig 3.png"></figure>
        <figure><img src="Fig 4.png"></figure>
    </div>
</section>
<section>
    <h2>H2-3</h2>
    <div>
        <figure><img src="Fig 5.png"></figure>
        <section>
            <h3>H3-3</h3>
            <div><figure><img src="Fig 6.png"></figure></div>
        </section>
    </div>
</section>
<section>
    <h2>H2-4</h2>
    <div>
    </div>
</section>
</body>

我想要的输出是:

代码语言:javascript
复制
H2-1
  H3-1
    Fig 1
H2-2
  Fig 2
  Fig 3
  Fig 4
H2-3
  Fig 5
  H3-3
    Fig 6

我想要找到显示所有有子图形元素的标题。如果标题有直接子图元素,我不想显示它。

我正在尝试的JSoup代码是:

代码语言:javascript
复制
Document doc = Jsoup.parse(sourceCode);
Elements sectionTags = doc.body().getElementsByTag("section");
for (Element sectTag : sectionTags)
{
  System.out.println (sectTag.children().first().ownText());  //print the Heading text 
  Elements figureTags = sectTag.getElementsByTag("figure");
  for (Element figTag : figureTags) 
  {
    System.out.println (figTag.getElementsByTag("img").attr("src").toString());  // print the image name
  }                          
}

但我没有得到理想的输出。我得到的输出是:

代码语言:javascript
复制
H2-1
  Fig 1
  H3-1
    Fig 1
H2-2
  Fig 2
H2-3
  Fig 5
  H3-3
    Fig 6

有什么帮助吗?我是JSoup的新手,我很感激任何能起作用的建议或建议。

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-09 06:14:45

你可以试试这个

代码语言:javascript
复制
/*function to parse the HTML*/
private void parseHTML(String szHTML){
    Document doc = Jsoup.parse(szHTML);

    Elements arrEle = doc.select("section");

    for(Element child: arrEle){
        printSectionDetails(child);
    }
}

/*function to print section details : prints direct child header name and img src of the section node */
private void printSectionDetails(Element section){
    Elements arrChildren = section.children();
    for(Element child : arrChildren){
        if(isHeader(child.tagName())){
            System.out.println(child.text());
        }

        if(child.tagName().equals("div")){
            Elements children = child.children();

            for(Element grandchild : children){
                if(grandchild.tagName().equals("figure")){
                    Elements arrImgs = grandchild.select("img");
                    for(Element img : arrImgs){
                        System.out.println(img.attr("src"));
                    }
                }
            }
        }
    }
}

/*checks whether the tag is a header*/
private boolean isHeader(String tagName) {
    if("h1".equalsIgnoreCase(tagName) || "h2".equalsIgnoreCase(tagName) || "h3".equalsIgnoreCase(tagName) || "h4".equalsIgnoreCase(tagName) || "h5".equalsIgnoreCase(tagName) || "h1".equalsIgnoreCase(tagName) || "h6".equalsIgnoreCase(tagName)){
        return true;
    }

    return false;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37682061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档