Meaningless Drivel is fun!*
The moose likes XML and Related Technologies and the fly likes DOM PArser: Reading value of node etc 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 "DOM PArser: Reading value of node etc" Watch "DOM PArser: Reading value of node etc" New topic
Author

DOM PArser: Reading value of node etc

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
I would like to know the meaning of various methods which DOM parser provides.

1) NodeList nodes=element.getChildNodes();
what is stored in nodes above?

2)Node node=nodes.item(i);// where i iterates over nodes.getLength();

what does item method do?

3)NodeList nlList=element.getElementsByTagName("Title").item(0).getChildNodes();

Why is "0" hardcoded in item?. What if i write "1" instead of "0"

4)Node nValue=(Node)nList.item(0)

What does above do??


Below is the xml file




Please explain


Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But you don't need to ask that question here. There is API documentation which tells you what the meaning of every single method in every single class is. Just go and read it.

I don't even need to provide a link, because the links are already automatically there in your post. Look at your original post and you'll see that the class names are actually links. Follow them.

Of course if you have questions about what you read there, don't hesitate to ask. But asking us to explain something for which the official explanation is readily available, that's just a waste of your time and ours.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 299
Thanks Paul. But i dont think there would be anyone who would be here in this forum without knowing how to find what a method does. Ofcourse, i can google the method name and find it out what it does. But what i wanted to knw was how element, node and node list differs.

Basic idea of providing the xml in my first post was to know how does node, element and node list differs. May be i was not categorical enough

Element i understand, for eg say <Title> tag in above eg.

Now say i do call a getDocumentElement() on the document object, hope i guess reference to the rootElement i.e <Library.>

Now, say with this root element, i do a getChildNodes(), then i guess i should get 2 nodes, i.e Book as there are two occurences of <book>

Now , i do
Node node=nodes.item(0), then i would get reference to the first "Book" Node. Now i want to read the element values like Title,
Author, etc , for which i would call getElementsByTagName with "tags" as parameters...


I dont know whether my understandinng is correct...Please verify.. Thanks for patience
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes, that sort of looks like an accurate description of what code like that might do if you ran it. But the question "What will happen when I run this code" is also one which there is no point in asking on a forum. It's much more straightforward to just run the code and see what happens.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM PArser: Reading value of node etc
 
Similar Threads
XPath Problem
XPath Problem
Parsing XML file using Xpath in jdk1.4
About parsing with DOM ...
XPath Problem