Java Utililty Methods XML Format

List of utility methods to do XML Format

Description

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

Method

voidprettyFormatXml(final InputStream xml, final OutputStream os, final int indent)
pretty Format Xml
try {
    final Source xmlInput = new StreamSource(xml);
    final StreamResult xmlOutput = new StreamResult(os);
    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", indent);
    final Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(xmlInput, xmlOutput);
...
StringprettyFormatXmlText(String text)
Format the xml-text string in a human-readable fashion.
Source xmlInput = new StreamSource(new StringReader(text));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
StringprettyPrint(final Source source)
Pretty print an XML.
try {
    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    StringWriter writer = new StringWriter();
    serializer.transform(source, new StreamResult(writer));
    return writer.toString();
} catch (TransformerException e) {
...
voidprettyPrint(String header, String xml)
pretty Print
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    if (header != null) {
        xml = "\n<" + header + ">" + xml + "</" + header + '>';
    transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out));
...
StringprettyPrintToString(String xml)
pretty Print To String
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = null;
try {
    transformer = TransformerFactory.newInstance().newTransformer();
} catch (TransformerConfigurationException e) {
    e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
...
StringprettyPrintXml(SOAPMessage message)
pretty Print Xml
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
return prettyPrintXml(out.toString("UTF-8"));
StringprettyPrintXml(String input)
pretty Print Xml
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(new StringReader(input)));
Element element = document.getDocumentElement();
element.normalize();
Writer writer = new StringWriter();
TransformerFactory factory = TransformerFactory.newInstance();
...
StringprettyPrintXml(String input)
pretty Print Xml
if (input == null) {
    return "";
int indent = 2;
try {
    Source xmlInput = new StreamSource(new StringReader(input));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
...
StringprettyPrintXML(String xml)
pretty Print XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
StreamSource source = new StreamSource(new StringReader(xml));
transformer.transform(source, result);
return result.getWriter().toString();
StringprettyXml(final String xml)
pretty Xml
try {
    final Document document = parseString(xml);
    cleanDocument(document);
    return toString(document, true, false);
} catch (final Exception e) {
    throw new RuntimeException(e);