File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Generate XML file without using DOM 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 "Generate XML file without using DOM" Watch "Generate XML file without using DOM" New topic
Author

Generate XML file without using DOM

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have managed to parse xml files using SAX and DOM and have currently created an xml file using DOM as well.
I have now been tasked with using another API to generate an xml file other than DOM.
I was wondering if it is posibble to use SAX to generate the XML file or if there is any other way? Obviously not by just printing lines to a file
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 123

jaxb is nice. object to xml, and vice versa.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Or you can use StAX to produce XML. Or you can use an XSLT identity transformation and feed SAX events into it.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42919
    
  68
XOM, dom4j, JDOM, ...
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Thanks guys, plenty for me to look into there. may try several if i have time.
Kathleen Angeles
Ranch Hand

Joined: Aug 06, 2012
Posts: 123

if i am not wrong, if you are a beginner in xml, stax is the answer for you in this case. look into it first. jaxb, xslt, etc. are solutions to more complicated situations.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
Kathleen Angeles wrote:if i am not wrong, if you are a beginner in xml, stax is the answer for you in this case. look into it first. jaxb, xslt, etc. are solutions to more complicated situations.

Currently using stax now actually. Seems very simple, i just don't seem to have the indentation like i do with DOM.
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 466
    
    1

Try XMLBean its very easy. You may also try XSLT. I have not tried other solutions and hence can't comment on them.


Vinod Tiwari | Twitter | Shikshanirman | Guftgu
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

tom davies wrote:Currently using stax now actually. Seems very simple, i just don't seem to have the indentation like i do with DOM.


DOM doesn't automatically produce pretty-printed documents either, if you're seeing that then either the indentation was already in the input document or you're running it through some other process which does indentation.

Indentation is just done by inserting white-space text nodes of the right sort in the right place. You'll need new-lines after end tags (and other places too, look at pretty-printed documents to see how its done) and you'll need a varying number of space characters before start tags depending on the depth of nesting. You could do this in StAX if you liked, I've done it when providing SAX events to a serializer, the complexity of what you would have to do is proportional to the complexity of your XML and the logic required to produce it.

From the data point of view, indentation is unnecessary. So if you're just producing the XML to be processed by some other computer system, then don't bother with it. You only need it to make the XML be more human-readable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate XML file without using DOM