Java 打印XML字符串时防止换行
我使用以下代码漂亮地打印XML字符串:Java 打印XML字符串时防止换行,java,xml,Java,Xml,我使用以下代码漂亮地打印XML字符串: private String prettyFormat(String xml) throws TransformerException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { String formattedString = null; try
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;
}
我遇到的问题是,它将长线包裹起来,因此:
<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:
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>
我通过
javax.xml.transform.Transformer实现了这一点
public static String formatXml(Document doc) {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
} catch (ClassCastException | TransformerException | TransformerFactoryConfigurationError e) {
throw new IllegalArgumentException("Cannot format xml", e);
}
}
似乎没有进一步的调整可用。您可以转而考虑使用自己的漂亮打印机,例如,如果没有换行后处理,则输出可能是另一种选择。-但是这种格式有什么不好的地方呢?对我来说,它非常漂亮。只需要做一个小小的更改,这一行必须更改为:
writer.serialize((元素)文档)代码>