简体   繁体   中英

How to parse XML with jsoup

I am trying to parse XML with jsoup, but I can't find any examples on this task.

My XML document looks like this:

<?xml version="1.0" encoding="UTF-8">
    <tests>
        <test>
            <id>xxx</id>
            <status>xxx</status>
        </test>
        <test>
            <id>xxx</id>
            <status>xxx</status>
        </test>
        ....
    </tests>
</xml>

It should be quite straightforward, but my attempt has failed.

Code:

Element content = doc.getElementById("content");
Elements tests = content.getElementsByTag("tests");
for (Element testElement : tests) {
    System.out.println(testElement.getElementsByTag("test"));
}

It seems the latest version of Jsoup (1.6.2 - released March 28, 2012) includes some basic support for XML.

String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"><tests><test><id>xxx</id><status>xxx</status></test><test><id>xxx</id><status>xxx</status></test></tests></xml>";
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
for (Element e : doc.select("test")) {
    System.out.println(e);
}

Give that a shot..

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