漂亮打印 XML 字符串时防止换行 [英] Prevent wrapping of lines when pretty printing XML string

查看:39
本文介绍了漂亮打印 XML 字符串时防止换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来漂亮地打印一个 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">

变成这样:

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

推荐答案

你不能.至少在使用 LSSerializer 时不会,因为它使用的 XMLSerializer 是私有的,并且 LSSerializer(及其实现 DOMSerializerImpl)没有任何设置 OutputFormat 属性的方法.但是,您可以直接使用 XMLSerializer:

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;
}

结果:

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

这篇关于漂亮打印 XML 字符串时防止换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆