tags:

views:

1316

answers:

3

We use SAX to parse XML because it does not require the entire XML document to be read into memory in order to parse a single value. I read many articles that insisted SAX can only be used to parse/decode XML and not create it. Is this true?

+1  A: 

No, it isn't true, you can encode XML to any Writer in Java using something similar to:

char[] ch;
AttributesImpl atts = new AttributesImpl();
Writer writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();

// SAX2.0 ContentHandler
TransformerHandler transformerHandler = tf.newTransformerHandler();

Transformer serializer = transformerHandler.getTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "nodes.dtd");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");

transformerHandler.setResult(streamResult);

transformerHandler.startDocument();

atts.clear();
// atts.addAttribute("", "", "xmlns", "CDATA", "http://www.example.com/nodes");
// atts.addAttribute("", "", "xmlns:xsi", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");
// atts.addAttribute("", "", "xsi:schemaLocation", "CDATA", "/nodes.xsd");
transformerHandler.startElement("", "", "node_list", atts);

// displayName element
if (displayName != null) {
 transformerHandler.startElement("", "", "display_name", null);
 ch = displayName.toCharArray();
 transformerHandler.characters(ch, 0, ch.length);
 transformerHandler.endElement("", "", "display_name");
}

// nodes element
transformerHandler.startElement("", "", "nodes", null);

atts.clear();
atts.addAttribute("", "", "node_type", "CDATA", "sometype");
transformerHandler.startElement("", "", "node", atts);

ch = node.getValue().toCharArray();
transformerHandler.startElement("", "", "value", null);
transformerHandler.characters(ch, 0, ch.length);
transformerHandler.endElement("", "", "value");

transformerHandler.endElement("", "", "node");

transformerHandler.endElement("", "", "nodes");

transformerHandler.endElement("", "", "node_list");

transformerHandler.endDocument();

String xml = writer.toString();
Grant Wagner
Does this build up the entire document in memory, by virtue of using the XSLT processor?
Steven Huwig
A: 

The SAX handler interfaces were designed to be easy to implement. It's easy to write a class with similar (perhaps wrapping a SAX interface) to make it easy to call - chaining, remembering which element to close, easier attributes, etc.

Tom Hawtin - tackline
A: 

see http://code.google.com/p/jlibs/wiki/XMLDocument

It supports creating xml document using SAX. both memory efficient and user friendly api

Santhosh Kumar T