JAXB anyType无法强制转换

时间:2017-06-07 15:56:11

标签: java xml soap jaxb

以下代码导致强制转换异常,我不确定原因。 ParameterValueList中的对象应该热切地解组为JAXB对象ParameterValueStruct,但事实并非如此。一切都是直接从soap编码和cwmp架构文件生成的。

有人有任何建议吗?

public static void main(String[] args) throws JAXBException, FileNotFoundException 
{
    JAXBContext c = JAXBContext.newInstance("org.dslforum.cwmp_1_1");

    Unmarshaller u = c.createUnmarshaller();
    Inform inform = (Inform) u.unmarshal(new FileInputStream("test.xml"));

    List<Object> list = inform.getParameterList().getAny();
    System.out.println(list); // prints [[ParameterValueStruct: null], ...

    for (Object o : list) {
        ParameterValueStruct pv = (ParameterValueStruct)o; // exception here
        System.out.println(pv.getName());
    }
}
  

线程“main”中的异常java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl无法强制转换为org.dslforum.cwmp_1_1.ParameterValueStruct       在Test.main(Test.java:26)

我有这个类是使用Java的xjc工具从http://schemas.xmlsoap.org/soap/encoding/生成的:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Array", propOrder = {
    "any"
})
@XmlSeeAlso({
    ParameterValueList.class,
})
public class Array {

    @XmlAnyElement(lax = true)
    protected List<Object> any;
    @XmlAttribute(name = "id")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected java.lang.String id;
    @XmlAttribute(name = "href")
    @XmlSchemaType(name = "anyURI")
    protected java.lang.String href;
    @XmlAttribute(name = "arrayType", namespace = "http://schemas.xmlsoap.org/soap/encoding/")
    protected java.lang.String arrayType;
    @XmlAttribute(name = "offset", namespace = "http://schemas.xmlsoap.org/soap/encoding/")
    protected java.lang.String offset;
    @XmlAnyAttribute
    private Map<QName, java.lang.String> otherAttributes = new HashMap<QName, java.lang.String>();

    public List<Object> getAny() {
        if (any == null) {
            any = new ArrayList<Object>();
        }
        return this.any;
    }
    ....
}

我也有这两个类也是通过相同的工具生成的,但来自https://www.broadband-forum.org/cwmp/cwmp-1-1.xsd

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ParameterValueList")
public class ParameterValueList
    extends Array
{
}

第二档:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ParameterValueStruct", propOrder = {
    "name",
    "value"
})
public class ParameterValueStruct { 
    @XmlElement(name = "Name", required = true)
    protected String name;
    @XmlElement(name = "Value", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected Object value;
    ...
}

这是我的test.xml文件:

<?xml version="1.0"?>
<cwmp:Inform xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ParameterList soap-enc:arrayType="cwmp:ParameterValueStruct[3]">
    <ParameterValueStruct>
      <Name>Device.DHCPv4.ClientNumberOfEntries</Name>
      <Value xsi:type="xsd:unsignedInt">0</Value>
    </ParameterValueStruct>
    <ParameterValueStruct>
      <Name>Device.DNS.Client.ServerNumberOfEntries</Name>
      <Value xsi:type="xsd:unsignedInt">1</Value>
    </ParameterValueStruct>
    <ParameterValueStruct>
      <Name>Device.DeviceInfo.AdditionalSoftwareVersion</Name>
      <Value xsi:type="xsd:string">DM: 532,SK: 2.6.33.9-rt31,SF: 251X 137.0,BK: 10</Value>
    </ParameterValueStruct>
  </ParameterList>
</cwmp:Inform>

1 个答案:

答案 0 :(得分:1)

您只需在@XmlRootElement(name="ParameterValueStruct",namespace="")

前添加ParameterValueStruct即可

您需要namespace=""

明确package-info.java
@javax.xml.bind.annotation.XmlSchema(namespace = "urn:dslforum-org:cwmp-1-1")
package org.dslforum.cwmp_1_1;

T 他正确的课程应如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ParameterValueStruct", propOrder = {
    "name",
    "value"
})
@XmlRootElement(name="ParameterValueStruct",namespace="")
public class ParameterValueStruct { 
    @XmlElement(name = "Name", required = true)
    protected String name;
    @XmlElement(name = "Value", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected Object value;
    ...
}

PS:有关@XmlAnyElement(lax=true)

的更好的JAXB教程,请参阅此http://blog.bdoughan.com/2012/12/jaxbs-xmlanyelementlaxtrue-explained.html