2017-06-07 93 views
2

下面的代碼導致了一個轉換異常,我不知道爲什麼。 ParameterValueList中的對象應該急切解組到JAXB對象ParameterValueStruct中,但事實並非如此。一切都是直接從soap編碼和cwmp模式文件生成的。JAXB anyType無法投射

有沒有人有任何建議?

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()); 
    } 
} 

在線程異常 「主要」 java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl不能轉換到org.dslforum.cwmp_1_1.ParameterValueStruct 在測試。主要(Test.java:26)

我有從http://schemas.xmlsoap.org/soap/encoding/使用Java的XJC工具生成的這個類:

@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; 
    } 
    .... 
} 

我一個LSO有哪些也通過相同的工具生成的這2類,但是從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> 

回答

2

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

這裏需要明確的namespace="" becauce的package-info.java

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

牛逼的他正確的類應該是這樣的:

@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:爲了更好地JAXB教程約@XmlAnyElement(lax=true)

看到這個 http://blog.bdoughan.com/2012/12/jaxbs-xmlanyelementlaxtrue-explained.html
+0

必須在您的建議中添加namespace =「」才能使其起作用。總共有:@XmlRootElement(name =「ParameterValueStruct」,namespace =「」) – ComputerEngineer88

+0

@ ComputerEngineer88我錯過了爲所有類設置命名空間的package-info.java,這裏是更新的答案。 – Edwardth

+0

我仍然不確定爲什麼我需要指定一個空的名稱空間。有沒有什麼辦法可以像ParameterValueStruct中的其他類那樣使用ParameterValueStruct?即不必指定一個空的名稱空間? – ComputerEngineer88