1

我需要解组 XML。

XML 是这样的:

<array>
    <item>
          <name>nick</pid>
          <age>18</provider>
    </item>
    <item>
          <name>gogo</pid>
          <age>20</provider>
     </item>

</array>

好的,这很好。现在我的java类是这样的:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "array")
public class Root{

    @XmlElement(name = "item", type = Provider.class)
    private List<Item> array = new ArrayList<Item>();

    ...

}

接着:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "item")

public class Item{

    private Integer age;
    private String name;
    ...

现在一切都很好!解组顺利!

但是当我有这样的 SOAP 响应时我该怎么办?

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://example.com/ns.xsd" xmlns:impl="http://example.com/impl.xsd">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns:ProviderResponse>
         <Status>OK</Status>

           <array>
                ...
              <item>
                 ...
              </item>
                 ...
           </array>


      </ns:ProviderResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

如何跳过 SOAP-ENV:Envelope、SOAP-ENV:Header、SOAP-ENV:Body,ns:ProviderResponse 标签?现在,使用这个 XML,当我试图解组时,我遇到了这样的错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.xmlsoap.org/soap/envelope/", local:"Envelope"). Expected elements are <{}array>,<{}item>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647) 
4

0 回答 0