【发布时间】:2014-12-23 13:31:11
【问题描述】:
我在使用 jaxb 映射类型为“object”的标记内容时遇到问题 我有一个名为“属性”的标签,在这个标签中,我有一个属性“类型”。此标记的内容可以是任何类型,我想获取此标记的“类型”属性和内容,这就是我创建 Value 类的原因,除了当我在字段“内容”上放置 XmlValue 注释时,我有一个 NullPointerException
我的 XML:
<attribute>
<id>9137755520013147917</id>
<name>Routeur</name>
<value xsi:type="EntityType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>9139898058113224747</id>
<name>pe-cdg1200e-2sn311-01</name>
<objectTypeId>9138560130813318972</objectTypeId>
<objectTypeName>Routeur PE</objectTypeName>
<classId>3061830882013808530</classId>
<className>Elément de Réseau</className>
</value>
</attribute>
我的标签属性可以包含任何类型的内容
Attribute.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"id",
"name",
"ismultiple",
"type",
"value"
})
public static class Attribute {
@XmlElement(required = true)
protected BigInteger id;
protected String name;
protected String ismultiple;
protected String type;
@XmlElement(required = true)
protected Value value;
Value.java
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public class Value {
public Value() {
}
@XmlAttribute(namespace="http://www.w3.org/2001/XMLSchema-instance")
private String type;
@XmlValue
private Object value ;
//getter setter
我有这个错误:
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154)
at com.sun.xml.internal.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:66)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:95)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:97)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:132)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at test.JAXB.main(JAXB.java:25)
公共类 JAXB {
/**
* @param args
* @throws JAXBException
*/
public static void main(String[] args) throws JAXBException {
File xmlFile = new File("C:\\activation.xml");
//JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
JAXBContext jaxbContext = JAXBContext.newInstance(ServiceOrderType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ServiceOrderType scf = (ServiceOrderType) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(scf);
for (Attribute attribute : scf.getAttributes().getAttribute()) {
if (attribute.getName().equals("RESEAU SERVEUR XML")) {
/*if (attribute.getValue() instanceof EntityListType){
EntityListType reseau = (EntityListType) attribute.getValue();
System.out.println( "name: " + reseau.getEntity().get(0).getName());
//Element content = (Element) attribute.getValue();
//System.out.println(content.getAttributes().toString());
}*/
System.out.println("value type: " + attribute.getValue().getType());
//List<EntityType> reseau = (List<EntityType>) attribute.getValue;
EntityListType reseau = (EntityListType) attribute.getValue().getObject();
System.out.println("*******>" + reseau.getEntity().get(0).getName());
// Object ob = attribute.getValue().getObject();
System.out.println(attribute.getName() + "---------------->" + attribute.getValue().getObject());
}
}
/*
for(Attribute attribute : scf.getComponents().getEntity().get(0).getAttributes().getAttribute()){
if(attribute.getName().equals("RESEAU SERVEUR XML")){
System.out.println(attribute.getName() + "---------------->" + attribute.getValue());
//System.out.println(attribute.getValue().get);
}
}*/
}
【问题讨论】:
-
这个
Nullpointer由@XmlValue在private Object value;上派生而来......但我不明白你想要什么......你能添加更多细节吗?为了更好地帮助你。 :) -
我想获取对象值中xml标签“属性”的内容:“私有对象值;”,
-
删除
@XmlValue到@XmlAnyElement应该可以正常工作 -
它不起作用,它在解组时抛出一个 classCastException。”线程“main”java.lang.ClassCastException 中的异常:javax.xml.bind.JAXBElement 无法转换为 test.ServiceOrderType”。代码:ServiceOrderType scf = (ServiceOrderType) jaxbUnmarshaller.unmarshal(xmlFile);
-
你能添加你的整个例子吗?