Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用HTML解析器在Java中获取div标记或其他标记中的内容_Java_Html_Html Parsing - Fatal编程技术网

如何使用HTML解析器在Java中获取div标记或其他标记中的内容

如何使用HTML解析器在Java中获取div标记或其他标记中的内容,java,html,html-parsing,Java,Html,Html Parsing,我想在标记中获取文本,即 <div id="title"> MotoGP </div> 看起来像这样: Parser p; // initialize p somehow p = createParser(html /* actual html String */, charset /* null for default */); NodeList nl = p.extractAllNodesThatMatch( new HasAttrib

我想在标记中获取文本,即

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

看起来像这样:

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());
特别见:


NodeList nodes=parser.extractAllNodesThatMatch(新的AndFilter(新的标记名过滤器(“div”)、新的HasAttributeFilter(“id”、“title”);SimpleNodeInterator NodeInterator=nodes.elements();而(nodeIterator.hasMoreNodes()){//Node Node=nodeIterator.nextNode();HeadingTag=(HeadingTag)Node;System.out.println(tag.getStringText());我已经编辑了我的问题并粘贴了我的代码。它不起作用。它不会在div之间返回数据
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());