javax.xml.bind.UnmarshalException:意外元素(uri:"",local:" soap:Envelope")

时间:2017-04-05 21:45:51

标签: java web-services jaxb wsdl

您好我使用wsimport从WSDL生成了java类。但我已将响应写入文件* .xml。但现在我想读取这个xml文件并填充已经生成的java类。

我试过了:

JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(source);

JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(new File("file.xml"));

Report是我在发送请求时作为回复获得的类

在第一种情况下我得到了

javax.xml.bind.UnmarshalException: unexpected element (uri: "", local:"soap:Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+

在第二种情况下

javax.xml.bind.UnmarshalException: unexpected element (uri: "http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope") Expected elements are: (<{"http://pagewhereisthewsdl.com"}CLASSES>)+

XML就像这样:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
              <ns3:GetReportOnlineResponse xmlns:ns2="http://pagewhereisthewsdl.com/document" xmlns:ns3="http://pagewhereisthewsdl.com/endpoint">
                 <ns2:Report>
                       ...
                 </ns2:Report>
           </ns3:GetReporteOnlineResponse>
       </soap:Body>
</soap:Envelope>

或者我该怎么办?

1 个答案:

答案 0 :(得分:1)

我相信你没有考虑SOAP信封。您需要先提取正文内容。

String xml = "<INSERT XML>";
SOAPMessage message = MessageFactory.newInstance().createMessage(null, new ByteArrayInputStream(xml.getBytes()));
JAXBContext jc = JAXBContext.newInstance(Report.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Report rc = (Report) unmarshaller.unmarshal(message.getSOAPBody().extractContentAsDocument());