Search code examples
javaandroidxmljsoup

Parsing xml with multi childs using jsoup


I have an xml file that looks as follows - link.

I would like to get the title from it.

In order to do so, I did the following:

Document bookDoc = Jsoup.connect( url ).parser( Parser.xmlParser() ).get();
Node node  = bookDoc.childNode( 2 ).childNode( 3 ).childNode( 3 );

This returns me this:

enter image description here

Now I have 2 questions:

  1. Isnt there any simpler way to get this title instead of using all of these childNodes? My worry is that in some result the title wont exactly be at childNode(3) and all my code wont work.

  2. How do I eventually get this title? Im stuck at this point and cant get the string of the title.

Thank you


Solution

  • You can use selectors to access elements. Here you want to select by tag name. Two ways to get the element you want:

    String title1 = bookDoc.select("record>display>title").text();
    String title2 = bookDoc.selectFirst("record").selectFirst("display").selectFirst("title").text();
    

    If you want to select more complicated things read:
    https://jsoup.org/cookbook/extracting-data/dom-navigation
    https://jsoup.org/cookbook/extracting-data/selector-syntax
    But you probably won't need them for parsing this XML.