Java Utililty Methods XML Document Format

List of utility methods to do XML Document Format

Description

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

Method

voidformatXML(Document xml, Document xsl, URIResolver resolver, Writer output)
format XML
try {
    DOMSource xslSource = new DOMSource(xsl);
    DOMSource xmlSource = new DOMSource(xml);
    Result result = new StreamResult(output);
    formatXML(xmlSource, xslSource, resolver, result);
} finally {
    output.close();
voidpretty(Document document, OutputStream outputStream, int indent)
pretty
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
    transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
    throw new RuntimeException(e);
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
...
voidprettyFormat(Document doc)
As a workaround for javax.xml.transform.Transformer not being able to pretty print XML.
prettyFormat(doc, DEFAULT_IDENT);
StringprettyPrint(Document doc)
pretty Print
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer;
StringWriter writer = new StringWriter();
try {
    serializer = tfactory.newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 
    serializer.transform(new DOMSource(doc), new StreamResult(writer));
...
voidprettyPrint(Document doc, String programID, String xmlTraDestinationFolderPath)
pretty Print
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new FileWriter(
        new File(xmlTraDestinationFolderPath + programID.replaceAll("_", "-") + ".xml"));
tf.transform(new DOMSource(doc), new StreamResult(out));
StringprettyPrint(final Document aNode)
pretty Print
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSOutput domOutput = impls.createLSOutput();
domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
StringWriter writer = new StringWriter();
domOutput.setCharacterStream(writer);
LSSerializer domWriter = impls.createLSSerializer();
DOMConfiguration domConfig = domWriter.getDomConfig();
...
voidprettyPrintXml(Document doc)
pretty Print Xml
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(doc);
Result dest = new StreamResult(System.out);
aTransformer.transform(src, dest);
StringprettyPrintXml(Document document)
Pretty prints the document to string using default charset.
return prettyPrintXml(document, "UTF-8");
StringprettyPrintXMLDocument(Node node)
Create a pretty String representation of a DOM Document
if (node == null) {
    return "";
DocumentBuilder builder = null;
try {
    builder = getDocumentBuilder();
} catch (ParserConfigurationException exception) {
    System.err.println(exception);
...
voidprint(Document d)
print
try {
    TransformerFactory xformFactory = TransformerFactory.newInstance();
    javax.xml.transform.Transformer idTransform = xformFactory.newTransformer();
    idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
    Source input = new DOMSource(d);
    Result output = new StreamResult(System.err);
    idTransform.transform(input, output);
} catch (TransformerConfigurationException e) {
...