2

我正在尝试使用 htmlparser 库解析 HTML 字符串。html是这样的:

<body>
        <div class="Level1">
            <div class="row">
                <div class="txt">
                    Date of analysis:
                </div><div class="content">
                    02/03/11
                </div>
            </div>
        </div><div class="Level1">
            <div class="row">
                <div class="txt">
                    Site:
                </div><div class="content">
                    13.0E
                </div>
            </div>
        </div><div class="Level1">
            <div class="row">
                <div class="txt">
                    Network type:
                </div><div class="content">
                    DVB-S
                </div>
            </div>
        </div>
</body>

我需要为给定的“txt”提取“内容”信息。我已经制作了一个过滤器,它返回带有 class="level1" 的 div,但我不知道如何使用 div 的内容制作过滤器,我的意思是如果 txt 的值为 Site: 然后读取 13.0 之类的内容E.

  NodeList nl = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"), new HasAttributeFilter("class", "Level1")));
       

有人可以帮我解决这个问题吗?如何在 div 中读取 div?谢谢!!

4

1 回答 1

0
NodeList nl = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"), new HasAttributeFilter("class", "Level1")));

better to do it like this:

NodeList nl = parser.parse(null); // you can also filter here

NodeList divs = nl.extractAllNodesThatMatch(
  new AndFilter(new TagNameFilter("DIV"), 
    new HasAttributeFilter("class", "txt")));

if( divs.size() > 0 ) {
  Tag div = divs.elementAt(0);
  String text = div.getText(); // this is the text of the div
}
于 2012-05-09T11:05:50.507 回答