Java XML Format xmlFormat(String xml)

Here you can find the source of xmlFormat(String xml)

Description

xml Format

License

Open Source License

Declaration

public static String xmlFormat(String xml) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayInputStream;
import java.io.IOException;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static String xmlFormat(String xml) {
        return xmlFormat(xml, 4);
    }//from ww  w . j  a  v a2s .c  o  m

    public static String xmlFormat(String xml, int indent) {
        StringWriter stringWriter = new StringWriter();

        try {
            /* Turn xml string into a document */
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));

            /* Remove whitespace outside tags */
            XPath xPath = XPathFactory.newInstance().newXPath();
            NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                    XPathConstants.NODESET);

            for (int i = 0; i < nodeList.getLength(); ++i) {
                Node node = nodeList.item(i);
                node.getParentNode().removeChild(node);
            }

            /* Setup pretty print options */
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setAttribute("indent-number", indent);
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            transformer.transform(new DOMSource(document), new StreamResult(stringWriter));

        } catch (XPathExpressionException | DOMException | IllegalArgumentException | SAXException | IOException
                | ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
            e.printStackTrace();
        }

        return stringWriter.toString();
    }
}

Related

  1. printNodeToConsole(Node n, ByteArrayOutputStream byteArrayOS)
  2. printReply(SOAPMessage reply)
  3. printStream(InputStream is)
  4. printXML(Node node)
  5. xmlFormat(String xml)