Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading/Writing/Manipulating XML using SAX

 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm working on part of an application where I need to manipulate XML. My basic problem is that I need to read an XML document, present the information to the user, allow the user to modify it, and then write that to a new file.
The XML represents bill information, the thing is I need to work with only one bill at a time, and each XML file could have an indefinate number of these bills, from 1 to 20,000+. So start with bill 1, get the data, let the user modify it if need be then save it to a new file and move on to the next bill, writing that one to the end of the same file.
My approach was to define my own "element" class that stores information on an element, name, namespace, value, attributes, parent element, child elements, etc. and pass these from the SAX handler to another class which keeps them in a collection. Then I work with that class to get and change the information I need.
This seems to work pretty well and the only real problem I've had is dealing with the output, I have to create a new file and write an indefinate number of bills to it one bill at a time. I managed to do it using a FileWriter but the code is pretty shitty and its only a temporary solution.
My question is... how can I do this better? I'm just looking for any suggestions from scratching my entire approach and doing something different to just finding a good way to handle the output. Any help is appreciated.
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SAX is read-only. Why don't you use JDOM, which only load data into memory when you need them, it can handle large files.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I kinda liked your first approach parsing one bill at a time out of SAX. But why make an "element" style class? Make a real business object that could be used with or without XML, and then something else that can turn it back into XML when done. Here's a write up I did on Reusable Sax Parsers that work this way.
On the other hand, I've never touched JDOM. It might do a spectacular job ... sounds like there may be lazy instantiation to keep from building the whole DOM in memory at once.
How does this approach fit with your users? Do they have to work the entire file at one sitting? Is it a requirement to be able to stop half way through and pick up again later?
[ July 20, 2003: Message edited by: Stan James ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic