Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

java - 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"));
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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..


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...