Java 如何打印格式化的SOAPMessage?
我有一个Java 如何打印格式化的SOAPMessage?,java,xml,soap,javax.xml,Java,Xml,Soap,Javax.xml,我有一个SOAPMessage(可以在javax.xml.soap.SOAPMessage中找到) 然而,打印它的唯一方法似乎是soapMessage.writeTo(System.out)但是,这没有任何新行,并且使用大的SOAPMessage可能很难阅读 此外,使用System.out.println(soapMessage.toString())仅打印出: com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl@76c7
SOAPMessage
(可以在javax.xml.soap.SOAPMessage
中找到)
然而,打印它的唯一方法似乎是soapMessage.writeTo(System.out)代码>但是,这没有任何新行,并且使用大的SOAPMessage
可能很难阅读
此外,使用System.out.println(soapMessage.toString())代码>仅打印出:
com.sun.xml.internal.messaging.saaj.soap.ver1_1.Message1_1Impl@76c7e77a
我查看了,但都没有解决换行和/或格式化SOAPMessage
的问题。如果您不介意向项目中添加其他依赖项,那么jdom为XML提供了良好的格式化输出
上的文档非常值得一看
这有点复杂,但您可以将XML写入JDOM文档对象,然后使用XMLOutputter对象以漂亮的格式打印它
// write the SoapMessage to a String called xml
File file= new File(pathToFile);
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
soapMessage.writeTo(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
SAXBuilder b = new SAXBuilder();
Document doc = b.build(file);
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
xmlOutputter.output(doc, System.out);