Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JAXB获取任何类型的内容和标记XML的属性_Java_Xml_Jaxb - Fatal编程技术网

Java 使用JAXB获取任何类型的内容和标记XML的属性

Java 使用JAXB获取任何类型的内容和标记XML的属性,java,xml,jaxb,Java,Xml,Jaxb,我在使用jaxb将标记的内容映射为“object”类型时遇到问题 我有一个名为“attribute”的标记,在这个标记中,我有一个属性“type”。这个标记的内容可以是任何类型,我想得到这个标记的“type”属性和内容,这就是为什么我创建了Value类,除了当我在字段“content”上放置XmlValue注释时,我有一个NullPointerException 我的XML: <attribute> <id>9137755520013147917</id&g

我在使用jaxb将标记的内容映射为“object”类型时遇到问题 我有一个名为“attribute”的标记,在这个标记中,我有一个属性“type”。这个标记的内容可以是任何类型,我想得到这个标记的“type”属性和内容,这就是为什么我创建了Value类,除了当我在字段“content”上放置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>
Value.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;
@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)
线程“main”java.lang.NullPointerException中的异常 位于com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154) 位于com.sun.xml.internal.bind.v2.runtime.property.ValueProperty.(ValueProperty.java:66) 位于com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:95) 位于com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(ClassBeanInfoImpl.java:145) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498) 位于com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(SingleElementNodeProperty.java:90) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:513) 位于com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113) 位于com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(ClassBeanInfoImpl.java:145) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498) 位于com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.(ArrayElementProperty.java:97) 位于com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty。(ArrayElementNodeProperty.java:47) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:513) 位于com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113) 位于com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(ClassBeanInfoImpl.java:145) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498) 位于com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty。(SingleElementNodeProperty.java:90) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:513) 位于com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113) 位于com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl。(ClassBeanInfoImpl.java:145) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) 位于com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.(ClassBeanInfoImpl.java:132) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:305) 位于com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100) 位于com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143) 位于com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处 位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)中 位于java.lang.reflect.Method.invoke(Method.java:597) 位于javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202) 位于javax.xml.bind.ContextFinder.find(ContextFinder.java:376) 位于javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574) 位于javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522) 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);
     }
     }*/
}
/**
*@param args
*@jaxbeexception
*/
公共静态void main(字符串[]args)抛出JAXBEException{
File xmlFile=新文件(“C:\\activation.xml”);
//JAXBContext JAXBContext=JAXBContext.newInstance(ObjectFactory.class);
JAXBContext JAXBContext=JAXBContext.newInstance(ServiceOrderType.class);
解组器jaxbUnmarshaller=jaxbContext.createUnmarshaller();
ServiceOrderType scf=(ServiceOrderType)jaxbUnmarshaller.unmarshal(xmlFile);
系统输出打印项次(scf);
对于(属性:scf.getAttributes().getAttribute()){
if(attribute.getName().equals(“RESEAU SERVEUR XML”)){
/*if(EntityListType的attribute.getValue()实例){
EntityListType reseau=(EntityListType)属性。getValue();
System.out.println(“名称:”+reseau.getEntity().get(0.getName());
//元素内容=(元素)属性.getValue();
//System.out.println(content.getAttributes().toString());
}*/
System.out.println(“值类型:”+attribute.getValue().getType());
//列表重置=(列表)属性
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
class Value {

    @XmlAttribute(namespace="http://www.w3.org/2001/XMLSchema-instance")
    private String type;

    @XmlAnyElement(lax=true) 
    private Object[] body;

    public String getType() {
        return type;
    }


    public Object[] getBody() {
        return body;
    }
}
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(example.getAttributes().get(0).getValue(), sw);
System.out.println(sw.toString());