aspose file tools*
The moose likes Servlets and the fly likes DOM or SAX for accessing an XML data structure? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "DOM or SAX for accessing an XML data structure?" Watch "DOM or SAX for accessing an XML data structure?" New topic
Author

DOM or SAX for accessing an XML data structure?

Daniel Walker
Greenhorn

Joined: Aug 09, 2001
Posts: 19
Hi there. I'm looking for some advice, regarding my choice of XML parser.
I am developing a servlet based system for storing details of publications, which registered users can log into and submit details of their papers. Other users can then view these details, through a field search. The publication details are held in an XML file (an XML database, if you will).
I need to decide whether to use the SAX or DOM API to do all of this. I need to be able to search the XML file for various combinations of search fields, edit records already entered, and delete records already entered - so my initial plan was to use the DOM.
However, I cannot guarantee the size of this XML file, as it will be added to by various people and could therefore become larger than the available memory on the server. Is this likely to be a major problem?
Any advice gratefully recieved. Thanks very much.
[ February 02, 2002: Message edited by: Daniel Walker ]

<B>daniel.walker</B><BR><I>***bleed in your own light...<BR>...dream of your own life...***</I>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Seems to me that if there are going to be on-going modifications, you will have to use DOM. If the data gets really large you will probably have to use one of the database programs that stores chunks of XML documents.
Bill
Daniel Walker
Greenhorn

Joined: Aug 09, 2001
Posts: 19
Thanks William
Yeah, I figured as much. The organisation I am doing this for wanted it done with XML, primarily so they didn't have to fork out for a database server, but I am beginning to feel that we are going to run into problems with this approach. It really is crying out for a database backend .
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

There are FREE databases out there (mysql) that work fairly well. mysql trumpets that they are used by NASA.

If you want to stay with XML, how about using JAXB. It's a data-binding API that has a few 'open' initiatives (Castor, Zeus) and one from sun in early-access release (JAXB API). It seems kinda cool. It allows you to 'marshall' an XML document into java objects. You create new objects or use the setXXXX() methods on existing object to make changes to your XML doc. Then you 'unmarshall' back into an XML document.

The propoenents of JAXB point out that it's less memory used DOM, because a parser does not get loaded into memory, only the data objects. And quicker than SAX.

http://java.sun.com/xml/jaxb/
The book I discovered JAXB in:
http://www.oreilly.com/catalog/javaxml2/
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Given the ease with which XML can be converted between formats, you could do a quick prototype using DOM or JDOM - if it later grows into a monster you can find other solutions.
Nothing like a quick prototype to let your client discover what they really wanted all along but could not express. Particularly if they are adding and removing fields, XML will be soooo much easier to work with.
I don't think JAXB offers any improvement but I am open to argument.
Bill
Daniel Walker
Greenhorn

Joined: Aug 09, 2001
Posts: 19
Thanks guys
Mike, JAXB does look very interesting, but I feel that because I am still dealing with this huge file (for searching through) the memory footprint will be fairly similar - essentially, a lot of the time I have to deal with the whole document at once (if I have got this completely wrong - and it wouldn't be the first time - please set me straight).
William, your comment about the ease with which additional fields can be added to an XML file has raised an important point, which I neglected to mention (or even consider) before: each publication in the XML file can be made up of different fields. It is based on the LaTeX standard (users will be able to upload their LaTeX bibliographies to the servlet for automatic conversion - though I haven't even begun to tackle this yet) so, in actuality, the fact that I am not tied into tables and rows with XML is a blessing.
I think I will use JDOM then. I have been informed it is slightly easier to work with than DOM, though it would be nice to use DOM if possible because of it's recent adoption into the J2SE API. I think I'd better get learning!
Thanks again for the help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
JDOM is likely to become a part of the standard Java library - furthermore I understand that it has a mechanism for creating a DOM if necessary.
JDOM should take less memory since it does not create as many objects as DOM for the same input.
It is interesting that so many XML projects are devoted to handling publication data, I also have one on the back burner.
Bill
Daniel Walker
Greenhorn

Joined: Aug 09, 2001
Posts: 19
Having looked at both DOM and JDOM, the ease of use factor has made my decision - JDOM. If it is coming over to the API anyway, great.
My publication project is being undertaken for academics - hence the XML (they do love their open standards, those academics). I agree, it does have a lot of advantages for publication data, and is very promising, but (IMHO) is still a little too unwieldy to be as good as my academics have been led to believe by hype & zeitgeist.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM or SAX for accessing an XML data structure?