<p>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?</p>

[{'answer_id': 73483, 'author': 'Grant Wagner', 'author_id': 9254, 'author_profile': 'https://Stackoverflow.com/users/9254', 'pm_score': 3, 'selected': True, 'text': '<p>No, it isn\'t true, you can encode XML to any Writer in Java using something similar to:</p>\n\n<pre><code>char[] ch;\nAttributesImpl atts = new AttributesImpl();\nWriter writer = new StringWriter();\nStreamResult streamResult = new StreamResult(writer);\nSAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();\n\n// SAX2.0 ContentHandler\nTransformerHandler transformerHandler = tf.newTransformerHandler();\n\nTransformer serializer = transformerHandler.getTransformer();\n\nserializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");\n// serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "nodes.dtd");\nserializer.setOutputProperty(OutputKeys.INDENT, "yes");\n\ntransformerHandler.setResult(streamResult);\n\ntransformerHandler.startDocument();\n\natts.clear();\n// atts.addAttribute("", "", "xmlns", "CDATA", "http://www.example.com/nodes");\n// atts.addAttribute("", "", "xmlns:xsi", "CDATA", "http://www.w3.org/2001/XMLSchema-instance");\n// atts.addAttribute("", "", "xsi:schemaLocation", "CDATA", "/nodes.xsd");\ntransformerHandler.startElement("", "", "node_list", atts);\n\n// displayName element\nif (displayName != null) {\n    transformerHandler.startElement("", "", "display_name", null);\n    ch = displayName.toCharArray();\n    transformerHandler.characters(ch, 0, ch.length);\n    transformerHandler.endElement("", "", "display_name");\n}\n\n// nodes element\ntransformerHandler.startElement("", "", "nodes", null);\n\natts.clear();\natts.addAttribute("", "", "node_type", "CDATA", "sometype");\ntransformerHandler.startElement("", "", "node", atts);\n\nch = node.getValue().toCharArray();\ntransformerHandler.startElement("", "", "value", null);\ntransformerHandler.characters(ch, 0, ch.length);\ntransformerHandler.endElement("", "", "value");\n\ntransformerHandler.endElement("", "", "node");\n\ntransformerHandler.endElement("", "", "nodes");\n\ntransformerHandler.endElement("", "", "node_list");\n\ntransformerHandler.endDocument();\n\nString xml = writer.toString();\n</code></pre>\n'}, {'answer_id': 73936, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/73476', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9254/']