繁体   English   中英

如何在Java中将XML插入soap头

[英]How to insert XML into soap header in Java

是否有一种干净的方式(没有字符串连接)将XML文档插入soap标头? 我使用JAXB来编译模式,现在我需要将它包装在soap信封中。 对于我使用的身体:

SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
soapMessage.getSOAPBody().addDocument(userDataDocument);

现在对于标题我还需要添加一个文档,但API没有

addDocument

以前我使用字符串连接,这很容易,但不是我脑海中最灵活的方式。 我不只是添加一个Qname而是整个XML文档。 有没有办法实现这个目标?

使用addDocument的源码计算出来...

public static SOAPBodyElement addDocumentToSoapHeader(Document document, SOAPMessage soapMessage) throws SOAPException {
    SOAPBodyElement newBodyElement = null;
    DocumentFragment docFrag = document.createDocumentFragment();
    Element rootElement = document.getDocumentElement();
    if(rootElement != null) {
        docFrag.appendChild(rootElement);
        Document ownerDoc = soapMessage.getSOAPHeader().getOwnerDocument();
        org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
        //this.addNode(replacingNode);
        soapMessage.getSOAPHeader().appendChild(replacingNode);

    }

    return newBodyElement;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM