0

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

1 Answer 1

1

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.

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

1 Comment

Thanks for helping me to under it mate

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.