Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate XML file without using DOM

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaxb is nice. object to xml, and vice versa.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you can use StAX to produce XML. Or you can use an XSLT identity transformation and feed SAX events into it.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XOM, dom4j, JDOM, ...
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, plenty for me to look into there. may try several if i have time.
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 466
1
IntelliJ IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try XMLBean its very easy. You may also try XSLT. I have not tried other solutions and hence can't comment on them.
 
Paul Clapham
Sheriff
Posts: 21137
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic