File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes Can SAX parser return Element object instead of Document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Can SAX parser return Element object instead of Document" Watch "Can SAX parser return Element object instead of Document" New topic
Author

Can SAX parser return Element object instead of Document

Qand Davaatsedev
Ranch Hand

Joined: Feb 06, 2002
Posts: 36
Hi,
To improve performance I want my SAX parser to return Element object instead of whole document. How can I efficiently implement this.
Thanks for your help.


Qand
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12783
    
    5
You are laboring under a misconception.
SAXparsers call handler routines with events that your code must interpret. Furthermore, an Element can't exist by itself, it must always have a parent Document. You could create an empty Document and build Elements from the events produced by your SAX parser.
Have you done experiments that indicate a performance problem with having a DOM created from your XML?
Bill
Qand Davaatsedev
Ranch Hand

Joined: Feb 06, 2002
Posts: 36
Hi Bill,
Thank you for your response.
Actually I did some small experiments with my code.
1) The document is not too big right now but it's supposed to grow fast.
Parsing and getting root element (let's say - clients) takes 1 second.
2) What really surprised me was the fact that getting particular client element (by an attribute) from the document took the same 1 second.
That's why I thought maybe there is some way around to avoid loading the whole document in a memory and get just the element I need.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12783
    
    5
You are on the right track if you ONLY need the contents of one relatively small element out of a big XML document. However, you won't get an Element - instead you will have to provide a "handler" to look at the SAX events. That handler will have to recognize the start of the element you want and provide for saving the data as it is parsed on the fly, presumably putting it in some sort of holder data structure or class.
Bill
Qand Davaatsedev
Ranch Hand

Joined: Feb 06, 2002
Posts: 36
I feel like I'm reinventing the wheel. It seems to be so obvious that parse method should be able to return some element...
My plan is:
1. To implement DefaultHandler and write element content into an input stream.
2. Parse new input stream.
Unfortunately it doesn't mean that parse() method won't create whole original document structure in a memory. This is what I would like to avoid.
Any thoughts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can SAX parser return Element object instead of Document