我有一个SOAP消息作为String,需要用Java解析它。我收到一个XML文件,其中包含XML数据内容的数据和签名,作为同一个soap消息的单独XML部分。以下是我收到的示例SOAP消息(不是有效的xml):
------=_Part_2074_1202079654.1337767440483
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: binary
Content-Id: <164914372224.1337767440483.IBM.WEBSERVICES@su79aas2.pinkroccade.lan>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header/>
<soapenv:Body>
<FileSet xmlns="http://www.mydomain.com">
<FileSetId>SRQ</FileSetId>
<File>
<FileType>SRQACK</FileType>
<FileName>ACK-BSP2-BP2-20120523100300-01.xml</FileName>
<FileContentId>1</FileContentId>
</File>
<File><FileType>SRQSIG</FileType>
<FileName>SIG-BSP2-BP2-20120523100300-01.sig</FileName>
<FileContentId>2</FileContentId>
</File>
</FileSet>
</soapenv:Body>
</soapenv:Envelope>
------=_Part_2074_1202079654.1337767440483
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: binary
Content-Id: 1
<?xml version="1.0" encoding="UTF-8"?>
<!--Handle SRQACK, SRSMUT, SUMMUT BP Postbank version 1.1-->
<Acknowledgements xmlns="http://www.mydomain.com">
<Header>
<BatchId>ACK-BSP2-BP2-20120523100300</BatchId>
<InterfaceId>ACK</InterfaceId>
<Version>02.0</Version>
<SourceSystemId>BSP2</SourceSystemId>
<TargetSystemId>BP2</TargetSystemId>
<CreateDateTime>2012-05-23T10:03:00Z</CreateDateTime>
<MessageCount>4</MessageCount>
</Header>
<Acknowledgement>
<BatchId>SRQ-BP2-BSP2-20120416101400</BatchId>
<AckTimeStamp>2012-05-23T10:03:00Z</AckTimeStamp>
<Result>ERROR</Result>
<ReasonCode>107</ReasonCode>
</Acknowledgement>
<Acknowledgement>
<BatchId>SRQ-BP2-BSP2-20120514140516</BatchId>
<AckTimeStamp>2012-05-23T10:03:00Z</AckTimeStamp>
<Result>OK</Result>
</Acknowledgement>
<Acknowledgement>
<BatchId>SRQ-BP2-BSP1-20120514140511</BatchId>
<AckTimeStamp>2012-05-23T10:03:00Z</AckTimeStamp>
<Result>ERROR</Result>
<ReasonCode>112</ReasonCode>
</Acknowledgement>
<Acknowledgement>
<BatchId>SRQ-BP2-BSP3-20120514140520</BatchId>
<AckTimeStamp>2012-05-23T10:03:00Z</AckTimeStamp>
<Result>ERROR</Result>
<ReasonCode>112</ReasonCode>
</Acknowledgement>
</Acknowledgements>
------=_Part_2074_1202079654.1337767440483
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: binary
Content-Id: 2
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--Handle SRQSIG, SRSSIG, EXCSIG, SUMSIG BSP 2 Biller version 1.2-->
<nl:Signature xmlns:nl="http://www.mydomain.com">
<SignatureValue>lJw2CNdcgEGEychSH/snpMvnXrV91775UANSZGN23n7hQnlIiak8Dqr9pe6FAtLjkZ9UZPip26VTxGVzeCHdsgYwrLt3mTLfX0h6KeRhPCYGd9QLxok1yv0Ua6iNlP0oEPaE4t6wJw/CKfCua4W0Jbnm18Ym5J7U08YB1+rc4Lw=</SignatureValue>
<Fingerprint>166105D2F2F23663F4405B6D84A4F51B48907F77</Fingerprint>
</nl:Signature>
------=_Part_2074_1202079654.1337767440483--
答案 0 :(得分:0)
第一部分是从文本中提取有效(从XML角度来看)片段。
您必须手动完成第一部分(=编写自己的代码),或者如果您很幸运,您可以找到一个可以为您完成的库。
第二部分 - 将这些片段提供给您使用的DOM解析器。
答案 1 :(得分:0)
使用多行正则表达式提取<ackowledgements*.</ackowledgements>
而另一个提取<nl:Signature.*<\nl:Signature>
并将这两个字符串提供给您选择的dom解析器 - 有关如何执行此操作的示例,请参阅In Java, how do I parse XML as a String instead of a file?