XML/DOM serialization: Difference between revisions

→‎{{header|NetRexx}}: As an aid to understanding, separate construction of DOM document from XML serialization
(→‎{{header|Java}}: As an aid to understanding, separate construction of DOM document from XML serialization)
(→‎{{header|NetRexx}}: As an aid to understanding, separate construction of DOM document from XML serialization)
Line 531:
import org.w3c.dom.
 
class RDOMSerialization public
do
factory = DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
impl = builder.getDOMImplementation()
doc = impl.createDocument(null, null, null)
e1 = doc.createElement("root")
e2 = doc.createElement("element")
doc.appendChild(e1)
e1.appendChild(e2)
e2.setTextContent("Some text here")
domSrc = DOMSource(doc)
txformer = TransformerFactory.newInstance().newTransformer()
txformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no")
txformer.setOutputProperty(OutputKeys.METHOD, "xml")
txformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
txformer.setOutputProperty(OutputKeys.INDENT, "yes")
txformer.setOutputProperty(OutputKeys.STANDALONE, "yes")
txformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")
 
properties private
sw = StringWriter()
domDoc = Document
sr = StreamResult(sw)
 
method main(args = String[]) public static
txformer.transform(domSrc, sr)
 
lcl = RDOMSerialization()
say sw.toString
lcl.buildDOMDocument()
lcl.serializeXML()
 
return
catch exPC = ParserConfigurationException
exPC.printStackTrace
catch exTC = TransformerConfigurationException
exTC.printStackTrace
catch exTF = TransformerFactoryConfigurationError
exTF.printStackTrace
catch exTE = TransformerException
exTE.printStackTrace
end
 
method buildDOMDocument() inheritable
return
 
do
factory = DocumentBuilderFactory.newInstance()
builder = factory.newDocumentBuilder()
impl = builder.getDOMImplementation()
doc domDoc = impl.createDocument(null, null, null)
e1 elmt1 = docdomDoc.createElement("root")
e2 elmt2 = docdomDoc.createElement("element")
e2 elmt2.setTextContent("Some text here")
 
e1 domDoc.appendChild(e2elmt1)
doc elmt1.appendChild(e1elmt2)
catch exPC = ParserConfigurationException
exPC.printStackTrace
end
return
 
method serializeXML() inheritable
 
do
domSrc = DOMSource(docdomDoc)
txformer = TransformerFactory.newInstance().newTransformer()
txformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no")
txformer.setOutputProperty(OutputKeys.METHOD, "xml")
txformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8")
txformer.setOutputProperty(OutputKeys.INDENT, "yes")
txformer.setOutputProperty(OutputKeys.STANDALONE, "yes")
txformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2")
sw = StringWriter()
sr = StreamResult(sw)
txformer.transform(domSrc, sr)
say sw.toString
 
catch exTC = TransformerConfigurationException
exTC.printStackTrace
catch exTF = TransformerFactoryConfigurationError
exTF.printStackTrace
catch exTE = TransformerException
exTE.printStackTrace
end
 
return
</lang>
 
Anonymous user