Java Utililty Methods XML Document to String

List of utility methods to do XML Document to String

Description

The list of methods to do XML Document to String are organized into topic(s).

Method

StringtoXmlString(Document document)
to Xml String
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
Transformer transformer;
try {
    transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource(document.getLastChild()), result);
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
...
Stringwrite(Document doc)
write
return write(doc.getDocumentElement());
voidwrite(Document doc, Result result)
write
TransformerFactory tfact = TransformerFactory.newInstance();
try {
    Transformer trans = tfact.newTransformer();
    trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    trans.setOutputProperty(OutputKeys.INDENT, "yes");
    trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    trans.setOutputProperty(OutputKeys.METHOD, "xml"); 
    trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
...
booleanwrite(String filename, Document document, boolean addDocType)
write
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    tf.setAttribute("indent-number", new Integer(4));
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    if (addDocType) {
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
                "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN");
...
voidwrite2xml(Document document)
writexml
try {
    Transformer ts = TransformerFactory.newInstance().newTransformer();
    ts.transform(new DOMSource(document), new StreamResult("xml/exam.xml"));
} catch (Exception e) {
    e.printStackTrace();
voidwrite2Xml(Document document)
write Xml
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName)));
voidwrite_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
Transforms a memory document with XML format into another with HTML format according an XSL file, and stores it in a file
TransformerFactory tFactory = TransformerFactory.newInstance();
File SalidaHTML = new File(htmlFile);
FileOutputStream os = new FileOutputStream(SalidaHTML);
Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));
DOMSource sourceId = new DOMSource(doc);
transformer.transform(sourceId, new StreamResult(os));
StringwriteDocument(Document doc, boolean omitXmlDeclaration)
Writes a document as a string.
String result = null;
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
...
voidwriteDocument(Document document)
write Document
try {
    DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();
    String documentStr = writer.writeToString(document);
    System.out.println("Serialized document: \n" + documentStr);
} catch (Exception e) {
    System.out.println("ERROR writing document: \n ");
...
voidwriteDocumentToStreamResult(Document document, StreamResult outputTarget)
write Document To Stream Result
try {
    Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.transform(new DOMSource(document), outputTarget);
} catch (TransformerConfigurationException ex) {
    throw new RuntimeException(ex);
} catch (TransformerException ex) {
    throw new RuntimeException(ex);
...