39

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"));
}
7
  • Did you read jsoup.org/cookbook/introduction/parsing-a-document? Commented Mar 27, 2012 at 9:22
  • 1
    @JavaCake : what have you tried so far? (If you are parsing xml, java is enough- no need for jsoup) Commented Mar 27, 2012 at 9:27
  • I have read that, but it does not answer my XML specific question. Commented Mar 27, 2012 at 9:29
  • 3
    @Jayan, i normally use the buildin XML parser, but it creates messy and fuzzy code (in my opinion), so i would rather try to use this API for once. Commented Mar 27, 2012 at 9:30
  • 1
    Make sure you take a look a JOOX: code.google.com/p/joox Commented Nov 26, 2013 at 11:25

1 Answer 1

89

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

Sign up to request clarification or add additional context in comments.

2 Comments

xom has a problem with unicode characters as data, and not parsing the document. This Jsoup solved my problem.
can we parse the large files with Jsoup?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.