简体   繁体   中英

How to use HTML parser to get whats in a div tag or another tag in Java

I want to get text in a tag, ie

<div id="title">    MotoGP  </div> 

I want to extract "MotoGP" from here. I'm using org.htmlparser .

I've tried

NodeList nodes = parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("div"),
     new HasAttributeFilter("id", "title")));

    SimpleNodeIterator nodeIterator = nodes.elements();
    while (nodeIterator.hasMoreNodes()) {

             HeadingTag tag = (HeadingTag)node;
             System.out.println(tag.getStringText());

Looks like something like this:

Parser p;

// initialize p somehow
p = createParser(html /* actual html String */,
    charset /* null for default */);

NodeList nl = p.extractAllNodesThatMatch(
    new HasAttributeFilter("id", "title")); // or other id...

// if you want the text of the 1st matching node:
System.out.println(nl.elementAt(0).getText());

See especially:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM