Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

XML DOM Parser Question  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just starting with DOM parser and have a question that is not clear from the examples I found or documentation. Assume I have the following XML structure:

In looking at the docs, it appears that if I want to get the leafs I would use 'doc.getElementsByTagName("leaf")'. Where 'doc' is the parsed document. However, it looks like I would get all the leafs from all the branches. Am I interpreting that correctly? If so, suppose I only wanted the leafs from the branch with id="1"? I am not finding any examples of extracting leafs, branch by branch. If someone knows of one, would you please post a link. Perhaps I am just not using the right keywords in my searches. TIA.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you have to get the root element from "Document".
Then get "NodeList" from root element.
Then you can iterate over the "NodeList" and do whatever you want to with the elements.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what 'doc.getElementsByTagName("leaf")' is supposed to return, right? The question is that it seems like the node list will contain all leafs. If I get just the node for branch id="1" that node does not seem to have a 'getElementsByTagName("leaf")' method. That is the part I am trying to figure out.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to use "getElementsByTagName("leaf")".
You can use "branch".
The NodeList returned will have all the information and you can iterate over it.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. That is what I would have thought but I cannot find how to extract child nodes from the branch nodes or iterate over its child nodes. That is the example I am looking for. All the ones I could find don't have duplicate child node names in different parent nodes.
 
Tim Holloway
Bartender
Posts: 18704
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not all Nodes can have children. Your branch Nodes are actually Elements, I believe, but the finder function returns ALL matching nodes and a simple enumeration of the first level below the root of the Document can return different types of Nodes such as ProcessingInstructions, Comments, and so forth.

So to use the getElementsByTagName on a sub-element Node, you have to cast it to Element. Although it's best to insure that the Node in question is in fact instanceof Element.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I think I get it now and have enough understanding to try some things.
 
abeer qamer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say icoderslab, it is a great place to get started they have a lot of programming tutorials and they’re all written in a coherent manner which are easily understandable.

check this out:

http://www.icoderslab.com/android-xml-parsing-in-android-studio-using-dom/?preview_id=512&preview_nonce=f428a0c359&_thumbnail_id=838&preview=true
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, abeer.  I couldn't get the URL to work until I took the params out of it:

http://www.icoderslab.com/android-xml-parsing-in-android-studio-using-dom/

 
abeer qamer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Welcome to the Ranch, abeer.  I couldn't get the URL to work until I took the params out of it:

http://www.icoderslab.com/android-xml-parsing-in-android-studio-using-dom/



thanks I'll edit it
 
abeer qamer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abeer qamer wrote:I would say icoderslab, it is a great place to get started they have a lot of programming tutorials and they’re all written in a coherent manner which are easily understandable.

check this out:

http://www.icoderslab.com/android-xml-parsing-in-android-studio-using-dom/
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!