用jsoup解析字符串

时间:2015-03-05 09:37:09

标签: parsing jsoup

我有一个字符串:

String HTMLtag="<xml><xslt><xhtml><whitespace><line-breaks>";

我想获得5个字符串:xml,xslt,xhtml,空格和换行符。

1 个答案:

答案 0 :(得分:0)

像这样的东西

String html = "<xml><xslt><xhtml><whitespace><line-breaks>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
for (Element e : doc.getAllElements()) {
    System.out.println(e.tagName());
}

输出

#root   --> This is the root element that is created by jsoup, you can ignore it.
xml
xslt
xhtml
whitespace
line-breaks

修改

String html = "<xml><xslt><xhtml><whitespace><line-breaks>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
for (Element e : doc.getAllElements()) {
    String tag = e.tagName();
    if(!tag.equalsIgnoreCase("#root"))
        System.out.println(tag);
}