JSoup不解析元素

时间:2016-10-28 19:23:26

标签: java jsoup

我正在尝试使用jsoup在下面列表的“”(info.net,test.com等)之间获取文本,并将它们添加到arraylist中。

<?xml version="1.0" encoding="UTF-8"?>
<supported-filehosts>
<host url="info.net"/>
<host url="test.com"/>
<host url="app.to"/>
</supported-filehosts>

似乎很简单,但我无法通过以下代码解决问题:

Document doc = Jsoup.parse(HOSTS);
Elements links = doc.select("host[url]");

for (Element link : links) {
    hostUrls.add(link.text());
}

有人可以看看。

1 个答案:

答案 0 :(得分:0)

您需要从每个代码中获取attribute

hostUrls.add(link.attr("url"));

示例代码:

public class Main { 

    public static void main(String[] args) throws IOException {


        final String HOSTS = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
            "<supported-filehosts>"+
            "<host url=\"info.net\"/>"+
            "<host url=\"test.com\"/>"+
            "<host url=\"app.to\"/>"+
            "</supported-filehosts>";
        List<String> hostUrls = new ArrayList<String>();

        Document doc = Jsoup.parse(HOSTS);
        Elements links = doc.select("host[url]");
        for (Element link : links) {
            hostUrls.add(link.attr("url"));
        }
        System.out.println(hostUrls.toString());
    }   
}  

输出:

  

[info.net,test.com,app.to]