【问题标题】:JSoup query: how to find all instances of an HTML tag in another tagJSoup查询:如何在另一个标签中找到一个HTML标签的所有实例
【发布时间】:2016-06-07 14:26:05
【问题描述】:

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

<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>

我想要的输出是:

H2-1
  H3-1
    Fig 1
H2-2
  Fig 2
  Fig 3
  Fig 4
H2-3
  Fig 5
  H3-3
    Fig 6

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

我正在尝试的JSoup代码是:

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
  }                          
}

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

H2-1
  Fig 1
  H3-1
    Fig 1
H2-2
  Fig 2
H2-3
  Fig 5
  H3-3
    Fig 6

有什么帮助吗?我是 JSoup 的新手,感谢任何可行的建议或提示。

提前致谢。

【问题讨论】:

  • 你能在这里贴一些代码吗?
  • 我发布了我在我的问题中尝试的 html 代码和 JSoup 代码。

标签: jsoup


【解决方案1】:

你可以试试这个

/*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;
}

【讨论】:

  • 谢谢。我会试试你的建议。
  • 是的,有点。抱歉回复晚了。根据你的脚本,我做了一些修改,到目前为止它正在工作。非常感谢您的专家帮助。
猜你喜欢
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 2013-04-23
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 2011-09-25
相关资源
最近更新 更多