当前位置: 首页 > 知识库问答 >
问题:

unmarshalException:意外元素(URI:“”,local:“soap:envelope”)

朱起运
2023-03-14

嗨,我已经使用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"));
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个答案

何华灿
2023-03-14

我相信你没有把肥皂信封考虑在内。你需要先提取身体的内容。

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());
 类似资料: