如何打印格式化的SOAPMessage?

4

我有一个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

我看了一下 如何在Java中美化打印XML?如何打印SOAPMessage如何将SOAPBody转换为字符串,但都没有解决换行和/或格式化SOAPMessage的问题。

1个回答

2
如果您不介意在项目中添加其他依赖项,那么jdom为XML提供了良好的格式化输出。可以查看jdom.org上的文档。您可以将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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接