• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

XML DOM

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you traverse through XML DOM using Java ?
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are so many XML DOM parser are available, at free of cost too.These parsers provide API for reading, extracting, editing data to and from a XML file in a very simple format in java language.
Mostly used DOM parsers are from Sun, IBM 's XML 4j , apaches xerces comes with IBM's parsers itself.
what basically parsers do , it allows you to traverse the XML file as a document in tree structure . you can easily create root node, add nodes to that ,add elements ,add attributes etc using different methods in Java.
i hope how it should make some sense to you.
thanx
Deepika

 
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do u want to work with DOM..
Follow the link for DOM tutorial.. http://java.sun.com/xml/docs/tutorial/dom/index.html
Anil

[This message has been edited by Anil Vupputuri (edited January 23, 2001).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhusudan,
I have written some code in java (IBM Visual Age) and traverse through the DOM to read in data. I start at the root element then get it's child nodes. Loop through the child nodes (and their children if applicable until you find a data holding element). IBM's parser has the methods getChildNodes() and getNodeName(). When you come upon that particular data holding element, extract the data. Inserting the data at this point into a data holding object makes sense.
You can loop through the XML DOM using hard coded values for the data element names you are seeking if you wish. I have used a recursive method to continue my search for the correct child node. (I get a list of data holding elements by reading in the DTD and adding the data holding element names to a list. I then compare this list to hard coded values in a data object as a check to see if the XML data will synch up with my data object):
protected void traverseDocumentNodes(node){
.............
.............
psuedo code if(node.element name != to data holding element names in my list){
traverseDocumentNodes(child node)
else
get the node's value and add to data object
}
}
The above is great when you have different levels of nesting of elements and data.
Hope this is of some use.
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on which data you wish to extract, there may be simpler methods, too. I have occasionally used a generic DOM-traverser to build a properties file in a simple "dotted" naming convention which then alows direct access to values if you know the structure. For example, the XML

would become something like:

Single values may then be fetched with a single access like
xml.getProperty("root.context");
and looping through multiple children is not hard either.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!