Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent wrapping of lines when pretty printing XML string

Tags:

java

xml

I am using the following code to pretty print an XML string:

private String prettyFormat(String xml) throws TransformerException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
     String formattedString = null;
     try {
         final InputSource src = new InputSource(new StringReader(xml));
         final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
         System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
         final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
         final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
         final LSSerializer writer = impl.createLSSerializer();
         writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); 
         writer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE); 
         formattedString = writer.writeToString(document); 

     } catch (Exception e) {
         throw new RuntimeException(e);
     }
   return formattedString;
}

The problem I have is that it wraps long lines so that this:

<message code="272" coeMsgName="CCR_I-Credit-Control-Initial" endtoend="AUTO" error="false" hopbyhop="AUTO" proxiable="true" request="true" retransmit="false">

becomes this:

<message code="272" coeMsgName="CCR_I-Credit-Control-Initial"
    endtoend="AUTO" error="false" hopbyhop="AUTO" proxiable="true"
    request="true" retransmit="false">
like image 329
eeijlar Avatar asked Jan 23 '26 19:01

eeijlar


1 Answers

You can't. At least not when using LSSerializer, because the XMLSerializer it uses is private, and LSSerializer (and its implementation DOMSerializerImpl) don't have any method of setting the OutputFormat attribute. You can however use an XMLSerializer directly:

private static String prettyFormat(String xml) throws TransformerException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
     String formattedString = null;
     try {
         final InputSource src = new InputSource(new StringReader(xml));
         final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();

         // the last parameter sets indenting/pretty-printing to true:
         OutputFormat outputFormat = new OutputFormat("WHATEVER", "UTF-8", true);
         // line width = 0 means no line wrapping:
         outputFormat.setLineWidth(0);
         StringWriter sw = new StringWriter();
         XML11Serializer writer = new XML11Serializer(sw, outputFormat);
         writer.serialize((Element)document);
         formattedString = sw.toString(); 

     } catch (Exception e) {
         throw new RuntimeException(e);
     }
   return formattedString;
}

Result:

<message code="272" coeMsgName="CCR_I-Credit-Control-Initial" endtoend="AUTO" error="false" hopbyhop="AUTO" proxiable="true" request="true" retransmit="false">
    <test/>
</message>
like image 108
Adrian Leonhard Avatar answered Jan 26 '26 09:01

Adrian Leonhard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!