gpt4 book ai didi

java - JAXB anyType 无法转换

转载 作者:数据小太阳 更新时间:2023-10-29 02:07:48 24 4
gpt4 key购买 nike

以下代码会导致转换异常,我不确定原因。 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());
}
}

Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to org.dslforum.cwmp_1_1.ParameterValueStruct at Test.main(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;
}
....
}

我也有这 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>

最佳答案

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

由于 package-info.java

,这里您需要明确的 namespace=""
@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:看这个http://blog.bdoughan.com/2012/12/jaxbs-xmlanyelementlaxtrue-explained.html有关 @XmlAnyElement(lax=true)

的更好的 JAXB 教程

关于java - JAXB anyType 无法转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44417560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com