在不创建新消息的情况下更新 AttachmentPart 内容

Updating the AttachmentPart contents without creating new message

我需要更新 SOAPMessage 中的 AttachmentPart 内容,如下图所示。我需要保持 headers 不变。
是否可以在不创建新的 SOAP 消息的情况下执行此操作?我正在使用 SAAJ API。

您能否使用 SOAPMessage.getAttachments() 调用 returns 所有附件部分的迭代器将附件拉入新对象,进行必要的修改,然后调用 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 会更容易吗?