0

我需要更新 SOAPMessage 中的 AttachmentPart 内容,如下图所示。我需要保持标题相同。
是否可以在不创建新的 SOAP 消息的情况下做到这一点?我正在使用 SAAJ API。

在此处输入图像描述

4

1 回答 1

1

您能否使用 SOAPMessage.getAttachments() 调用返回所有附件部分的迭代器以将附件拉入新对象,进行必要的修改,然后调用 SOAPMessage.removeAllAttachments() 函数从原始对象中清除对象消息并调用 addAttachmentPart(AttachmentPart) 函数重新添加更改的对象?

        SOAPMessage message = getSoapMessageFromString(foo);

        List<AttachmentPart> collectionOfAttachments = new ArrayList<AttachmentPart>();

        for (Iterator attachmentIterator = message.getAttachments(); attachmentIterator.hasNext()) {
            AttachmentPart attachment = (AttachmentPart) attachmentIterator.next();
            //**DO WORK HERE ON attachment**
            collectionOfAttachments.add(attachment);
        }

        message.removeAllAttachments();

        for (AttachmentPart newAttachment : collectionOfAttachments) {
            message.addAttachmentPart(newAttachment);
        }



 // This method takes an XML string as input and uses it to create a new
 // SOAPMessage object
 // and then returns that object for further use.
 private static SOAPMessage getSoapMessageFromString(String xml)
           throws SOAPException, IOException {

      MessageFactory factory = MessageFactory.newInstance();

      // Create a new message object with default MIME headers and the data
      // from the XML string we passed in
      SOAPMessage message = factory
                .createMessage(
                          new MimeHeaders(),
                          new ByteArrayInputStream(xml.getBytes(Charset
                                    .forName("UTF-8"))));
      return message;
 }

您希望对附件进行哪些更改?将主体保留在 DOM 对象中并一起创建一个新的 SOAPMessage 会更容易吗?

于 2015-02-27T20:55:50.103 回答