• Post Reply Bookmark Topic Watch Topic
  • New Topic

traverse same level nodes in an XML  RSS feed

 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have one query regarding XML. I have a XML file which is having few nodes at same level, like :




There is no root node above this level. this is how the data reaches to my application. I wanted to know the best optimal way to traverse all the A nodes, without going through B nodes. The current implementation which I have is to iterate all the nodes, then do element.getName().equals("A") and so on.. but the processing in this is too much, as I have this node, then I have to process it's many child nodes and get many attributes from these node and the child nodes.

I am not able to find a better logic for this. Please let me know what if what I am missing here..
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not exactly sure what you are trying to do, but why not use the getElementsByTagName("A") method in org.w3c.dom.Element interface?

If simplicity of coding is more important than speed, look into XPath - javax.xml.xpath package.

Bill
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for replying. well, i had thought about getting elements by tag name, did not try it. i have some 5000 nodes in the format which i have given. within each A node and B node, there will be some child nodes which will vary based on some conditions, I am looking at the most optimal way to traverse all the A nodes and it's child nodes once, without repeating anything or getting messed up in iteration.

i will start working on these soon, just wanted to plan out how i will be going through this task.

thanks
 
Paul Clapham
Sheriff
Posts: 22829
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:I am looking at the most optimal way to traverse all the A nodes and it's child nodes once, without repeating anything or getting messed up in iteration.


Well, you only have 5000 nodes so it's a pretty small file. So it isn't worth spending any time looking for the "most optimal" way. Just pick a reasonable way and carry on with that.
 
s ravi chandran
Ranch Hand
Posts: 579
6
Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
s ravi chandran wrote:I am looking at the most optimal way to traverse all the A nodes and it's child nodes once, without repeating anything or getting messed up in iteration.


Well, you only have 5000 nodes so it's a pretty small file. So it isn't worth spending any time looking for the "most optimal" way. Just pick a reasonable way and carry on with that.


Thanks for replying. Well, I have to show a quick demo to some client. So, i will be taking 5000 node A and some 3000 node B mixed up. But the actual client data may have millions of nodes of various types. so, optimization is one thing which I want to stress before designing, so that my implementation won't need to be changed in the later stage.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s ravi chandran wrote:...so, optimization is one thing which I want to stress before designing, so that my implementation won't need to be changed in the later stage...

In which case, optimization is the LAST thing (quite literally) you want to be doing, and certainly not before designing. Furthermore, if you design your demo correctly, you will be able to change the implementation to add optimization quite happily.

Michael A. Jackson wrote:The First Rule of Program Optimization: Don't do it.
The Second Rule of Program Optimization (for experts only!): Don't do it yet.

Winston

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!