com.vladsch.flexmark.docx.converter.util.XmlFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flexmark-docx-converter Show documentation
Show all versions of flexmark-docx-converter Show documentation
flexmark-java extension for rendering docx format
package com.vladsch.flexmark.docx.converter.util;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;
/**
* Pretty-prints xml, supplied as a string.
*
* eg.
* {@code
* String formattedXml = XmlFormatter.format("hello ");
* }
*
* StackOverflow: https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java
*/
public class XmlFormatter {
public static String format(String xml) {
try {
InputSource src = new InputSource(new StringReader(xml));
Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("