aspose file tools*
The moose likes XML and Related Technologies and the fly likes Reading a root node attribute 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 "Reading a root node attribute" Watch "Reading a root node attribute" New topic
Author

Reading a root node attribute

Robert Stevenson
Greenhorn

Joined: Oct 03, 2007
Posts: 3
Hi everyone!

I am totally new to XML and SAX and I am asking for your help!

Using java, I need to read the contents of an xml file to check if an attribute on the root node is present or not. If the attribute is present I want to store it.

I really have no idea as to how to do this and I am getting confused by looking at the on-line books and examples, can anyone please spell this out for me

thanks for your help

Robert
Robert Stevenson
Greenhorn

Joined: Oct 03, 2007
Posts: 3
Please help me! thanks
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
Robert,

Look for DOM/SAX parsing tutorials online.

Here is one from SUN

http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/dom/index.html

That sample explains you how to read a node from the XML file and then attributes..




And here is the some of the code I wrote if the input XML is in the String format. I was reading userName ...but the same way you can read all the elements and attributes from the node(s) ..

variable 'str' is an XML string.




Thanks,
Ugender
[ October 04, 2007: Message edited by: Ugender Rekulampally ]
Robert Stevenson
Greenhorn

Joined: Oct 03, 2007
Posts: 3
Thanks very much for the help,

I have been told that I will NEED to use SAX to do this because the files will be quite large in size, is this correct?

Thanks

Robert
Ugender Rekulampally
Ranch Hand

Joined: Nov 14, 2005
Posts: 130
Robert,
It depends. If files are really large, then yes SAX will be better. Since SAX parser won't read entire XML document into memeory but it reads as it parses so there you can see the advantage.

But the other side of the SAX is, it parses sequentially. if you are looking for few particular nodes, then it wastes lot of processor time by tranvelling all the nodes sequentially.

But using DOM will get you the required nodes without touching every element sequentially. But the problem with DOM is, it reads the entire document in the memory and builds a tree structure before it starts processing.

So your decision depends on How big the file is going to be and the order of the processing.

Thanks,
Ugender
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
Hi, Ugender Rekulampally

Every DOM parser uses SAX parser to build the DOM tree. Your argument of SAX parser wastes more time to access a few node does NOT hold any water.

However, there are many DOM or other parsers actually don't read the entire XML file into memory, they are using the Lazy-Initialization Pattern. They only expand the node when it is needed. In that case, the parser is better.

As far as I know, JDOM was using the Lazy-Initialization Pattern, even though JDOM stopped developing since 2004.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a root node attribute