Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing xml, Results missing

 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an xml.


I would like to parse it with this code:


This codes result is:
Root element: books
nList length: 1
a : DRIFT, by Rachel Maddow

I can not understand why the nList.getLength() value is 1.

Does someone have any idea how to change this code?
Thank you in advance for any help you can provide.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Bloom wrote:I can not understand why the nList.getLength() value is 1.

Because there is only one node named <booklist> present in the xml.
If you desire to retrieve the 'a' element, either get elements by the tag 'a' on the booklist element or use an XPath.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this discussion would fit better in our XML forum: moving.
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie! You are right! Thanks!

get elements by the tag 'a'

John Jai! Thank you for your idea, i'd like to try but if i change the code in the 9th line NodeList nList = doc.getElementsByTagName("a"); the nList.getLength() is 8, but i don't know how can i write the results.

Thank you in advance for any help you can provide.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Bloom wrote:but i don't know how can i write the results.

What is the result you want to get from the xml?
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:

Root element: books
nList length: 8
a : DRIFT, by Rachel Maddow
a : IMAGINE, by Jonah Lehrer
a : THE WITNESS, by Nora Roberts
a : FIFTY SHADES FREED, by E. L. James
a : LIFEBOAT NO. 8, by Elizabeth Kaye
a : UNNATURAL ACTS, by Stuart Woods
a : THE BIG MISS, by Hank Haney
a : THE PRESIDENTS CLUB, by Nancy Gibbs

And i can not use XPath, because i have to use JDK1.4 and the XPath exist since 1.5.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you are interested only in printing the "books" and text content of the tag named "a".

You have already printed "books" using getDocumentElement() method. Now instead of getting "booklist" element you can call the method getElementsByTagName("a") on the document and get the NodeList.

Now iterate the NodeList and for every node (every "a" node) print its text content (e.g. - DRIFT, by Rachel Maddow) by calling the getTextContent() method on the node.

Try to post the code if you face any issues.
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not believe this..: There is no getTextContent() method at the JDK 1.4. I found this method at the 1.5.. but as i've said i have to use this so old jdk version.

 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try the alternative getNodeValue(). But you must first parse to the text node (text content of the node 'a').



I tested it against Java 1.6 so not sure how it will work against 1.4
 
Edward Bloom
Ranch Hand
Posts: 34
Eclipse IDE Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is work so well!
Thank you so much your patience and your help!

Have a nice day!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic