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

OutputStreamapplyTransformation(InputStream xsltStream, Map xsltParameters, InputStream inputXmlStream, OutputStream outputStream)
Apply the XSLT transformation (xsltStream) to the xml given (inputXmlStream) and write the output to outputStream
return applyTransformation(new StreamSource(xsltStream), xsltParameters, inputXmlStream, outputStream);
Stringformat(final String xml)
Parse string as XML document and return string with reformatted document.
return toString(getXMLDocument(xml));
Stringformat(Node node)
format
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    StringWriter sw = new StringWriter();
    transformer.transform(new DOMSource(node), new StreamResult(sw));
...
Stringformat(Node node, String indent)
Format generated xml doc by indentation
StringBuilder formatted = new StringBuilder();
if (node.getNodeType() == Node.ELEMENT_NODE) {
    StringBuilder attributes = new StringBuilder();
    for (int k = 0; k < node.getAttributes().getLength(); k++) {
        attributes.append(" ");
        attributes.append(node.getAttributes().item(k).getNodeName());
        attributes.append("=\"");
        attributes.append(node.getAttributes().item(k).getNodeValue());
...
Stringformat(String unformattedXml)
format
try {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Transformer transformer;
    transformer = factory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(out);
    StreamSource source = new StreamSource(new ByteArrayInputStream(unformattedXml.getBytes()));
    transformer.transform(source, result);
...
StringformatAttributes(Node node)
Returns formatted attributes of the node.
StringBuilder sb = new StringBuilder();
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
    Node attr = attrs.item(i);
    sb.append(' ').append(attr.getNodeName()).append("= '").append(attr.getNodeValue()).append("'");
return sb.toString();
voidformattedPrint(Node xml, OutputStream out)
formatted Print
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer tf = tFactory.newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
StreamResult result = new StreamResult(new OutputStreamWriter(out, "UTF-8"));
...
StringformatXML(byte[] xmlData, int indent)
format XML
Source xmlInput = new StreamSource(new ByteArrayInputStream(xmlData));
StreamResult xmlOutput = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", indent);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
...
StringformatXML(String unformatted)
format XML
if (unformatted == null)
    return null;
String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><");
Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
StringformatXml(String xml)
Takes given XML and indents everything with 2 spaces.
try {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", 2);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...