2
如何使用JSoup按標籤獲取元素(http://jsoup.org/)?如何使用JSoup按標籤獲取元素? - java
我有以下的輸入,並要求下面的輸出,但我沒有得到<source>...<\source>
標籤內的文本:
[在:]
<html>
<something>
<source>foo bar bar</source>
<something>
<source>foo foo bar</source>
</html>
[所需出來:]
foo bar bar
foo foo bar
我試過這個:
import java.io.*;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class HelloJsoup {
public static void main(String[] args) throws IOException {
String br = "<html><source>foo bar bar</source></html>";
Document doc = Jsoup.parse(br);
//System.out.println(doc);
for (Element sentence : doc.getElementsByTag("source"))
System.out.print(sentence);
}
}
但它輸出:
<source></source>
感謝它的工作原理,但爲什麼添加xmlParser的工作? – alvas
'