File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Parsing xml, Results missing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing xml, Results missing" Watch "Parsing xml, Results missing" New topic
Author

Parsing xml, Results missing

Edward Bloom
Greenhorn

Joined: Apr 10, 2012
Posts: 29

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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 13, 2005
Posts: 38792
    
  23
I think this discussion would fit better in our XML forum: moving.
Edward Bloom
Greenhorn

Joined: Apr 10, 2012
Posts: 29

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
Bartender

Joined: May 31, 2011
Posts: 1776
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
Greenhorn

Joined: Apr 10, 2012
Posts: 29

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
Bartender

Joined: May 31, 2011
Posts: 1776
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
Greenhorn

Joined: Apr 10, 2012
Posts: 29

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
Bartender

Joined: May 31, 2011
Posts: 1776
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
Greenhorn

Joined: Apr 10, 2012
Posts: 29

Your code is work so well!
Thank you so much your patience and your help!

Have a nice day!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing xml, Results missing