How to pretty print XML from Java?

后端 未结 30 2510
慢半拍i
慢半拍i 2020-11-22 01:55

I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this?



        
相关标签:
30条回答
  • 2020-11-22 02:30
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    //initialize StreamResult with File object to save to file
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String xmlString = result.getWriter().toString();
    System.out.println(xmlString);
    

    Note: Results may vary depending on the Java version. Search for workarounds specific to your platform.

    0 讨论(0)
  • 2020-11-22 02:30

    a simpler solution based on this answer:

    public static String prettyFormat(String input, int indent) {
        try {
            Source xmlInput = new StreamSource(new StringReader(input));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(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();
        } catch (Exception e) {
            throw new RuntimeException(e); // simple exception handling, please review it
        }
    }
    
    public static String prettyFormat(String input) {
        return prettyFormat(input, 2);
    }
    

    testcase:

    prettyFormat("<root><child>aaa</child><child/></root>");
    

    returns:

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
      <child>aaa</child>
      <child/>
    </root>
    
    0 讨论(0)
  • 2020-11-22 02:30

    Just to note that top rated answer requires the use of xerces.

    If you don't want to add this external dependency then you can simply use the standard jdk libraries (which actually are built using xerces internally).

    N.B. There was a bug with jdk version 1.5 see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 but it is resolved now.,

    (Note if an error occurs this will return the original text)

    package com.test;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.sax.SAXSource;
    import javax.xml.transform.sax.SAXTransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    
    import org.xml.sax.InputSource;
    
    public class XmlTest {
        public static void main(String[] args) {
            XmlTest t = new XmlTest();
            System.out.println(t.formatXml("<a><b><c/><d>text D</d><e value='0'/></b></a>"));
        }
    
        public String formatXml(String xml){
            try{
                Transformer serializer= SAXTransformerFactory.newInstance().newTransformer();
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                //serializer.setOutputProperty("{http://xml.customer.org/xslt}indent-amount", "2");
                Source xmlSource=new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
                StreamResult res =  new StreamResult(new ByteArrayOutputStream());            
                serializer.transform(xmlSource, res);
                return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray());
            }catch(Exception e){
                //TODO log error
                return xml;
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:30

    I saw one answer using Scala, so here is another one in Groovy, just in case someone finds it interesting. The default indentation is 2 steps, XmlNodePrinter constructor can be passed another value as well.

    def xml = "<tag><nested>hello</nested></tag>"
    def stringWriter = new StringWriter()
    def node = new XmlParser().parseText(xml);
    new XmlNodePrinter(new PrintWriter(stringWriter)).print(node)
    println stringWriter.toString()
    

    Usage from Java if groovy jar is in classpath

      String xml = "<tag><nested>hello</nested></tag>";
      StringWriter stringWriter = new StringWriter();
      Node node = new XmlParser().parseText(xml);
      new XmlNodePrinter(new PrintWriter(stringWriter)).print(node);
      System.out.println(stringWriter.toString());
    
    0 讨论(0)
  • 2020-11-22 02:31

    Here's an answer to my own question. I combined the answers from the various results to write a class that pretty prints XML.

    No guarantees on how it responds with invalid XML or large documents.

    package ecb.sdw.pretty;
    
    import org.apache.xml.serialize.OutputFormat;
    import org.apache.xml.serialize.XMLSerializer;
    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import java.io.IOException;
    import java.io.StringReader;
    import java.io.StringWriter;
    import java.io.Writer;
    
    /**
     * Pretty-prints xml, supplied as a string.
     * <p/>
     * eg.
     * <code>
     * String formattedXml = new XmlFormatter().format("<tag><nested>hello</nested></tag>");
     * </code>
     */
    public class XmlFormatter {
    
        public XmlFormatter() {
        }
    
        public String format(String unformattedXml) {
            try {
                final Document document = parseXmlFile(unformattedXml);
    
                OutputFormat format = new OutputFormat(document);
                format.setLineWidth(65);
                format.setIndenting(true);
                format.setIndent(2);
                Writer out = new StringWriter();
                XMLSerializer serializer = new XMLSerializer(out, format);
                serializer.serialize(document);
    
                return out.toString();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private Document parseXmlFile(String in) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(in));
                return db.parse(is);
            } catch (ParserConfigurationException e) {
                throw new RuntimeException(e);
            } catch (SAXException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public static void main(String[] args) {
            String unformattedXml =
                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?><QueryMessage\n" +
                            "        xmlns=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/message\"\n" +
                            "        xmlns:query=\"http://www.SDMX.org/resources/SDMXML/schemas/v2_0/query\">\n" +
                            "    <Query>\n" +
                            "        <query:CategorySchemeWhere>\n" +
                            "   \t\t\t\t\t         <query:AgencyID>ECB\n\n\n\n</query:AgencyID>\n" +
                            "        </query:CategorySchemeWhere>\n" +
                            "    </Query>\n\n\n\n\n" +
                            "</QueryMessage>";
    
            System.out.println(new XmlFormatter().format(unformattedXml));
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 02:31

    This code below working perfectly

    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Source;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.stream.StreamResult;
    import javax.xml.transform.stream.StreamSource;
    
    String formattedXml1 = prettyFormat("<root><child>aaa</child><child/></root>");
    
    public static String prettyFormat(String input) {
        return prettyFormat(input, "2");
    }
    
    public static String prettyFormat(String input, String indent) {
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indent);
            transformer.transform(xmlInput, new StreamResult(stringWriter));
    
            String pretty = stringWriter.toString();
            pretty = pretty.replace("\r\n", "\n");
            return pretty;              
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    0 讨论(0)
提交回复
热议问题