Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can SAX parser return Element object instead of Document

 
Qand Davaatsedev
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic