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

最佳答案

您能否使用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会更容易吗?

10-08 18:23