Originally posted by Suresh Appagundi:
In JAXP library,
What is the difference among
SAXResult
DOMResult
StreamResult ?
And which is better to use?
The implementations of javax.xml.transform.Result are used as the "output stream" for javax.xml.transform.Transformer#transform(Source, Result).
SAXResult is a class which takes the transformation result and feeds it into the SAX handler you've given it (usually passed to it in its constructor).
DOMResult acts as a wrapper for the DOM tree generated by the transformation. After calling myTransformer.transform(mySource, myDomResult), you can do myDomResult.getNode() and it returns the resulting DOM tree's root node.
StreamResult acts as a serializer component by taking the result of the transformation and writing it into an OutputStream as "raw" XML.
Which of these
you should use is completely dependent on your situation and what you want to accomplish?
Originally posted by Suresh Appagundi:
I have HashMap with data filled in it. Now i need to generate XML file with contents in this HashMap. For doing this i want to use JAXP and SAX and serialize the data to a file.
You could write an implementation of ContentHandler which reacts to SAX events (startElement, characters, endElement, etc.) by writing matching XML constructs into an OutputStream and then just call the relevant methods for each entry in your HashMap.
Another option would be to create a DOM tree and then serialize that in the end using javax.xml.transform.Transformer.