Java JAXB 解组 @XmlAnyElement

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20329510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:07:12  来源:igfitidea点击:

JAXB Unmarshalling @XmlAnyElement

javaxml-parsingjaxbeclipselinkjaxb2

提问by hiddenuser

I have created three JAXB class : Home , Person , Animal. Java Class Home have variable List<Object> anythat may contain Person and/or Animal instance .

我已经创建了三个JAXB类:Home , Person , Animal。Java Class Home 具有List<Object> any可能包含 Person 和/或 Animal 实例的变量。

    public class Home {
        @XmlAnyElement(lax = true)
        protected List<Object> any;
    //setter getter also implemented
    }
@XmlRootElement(name = "Person")                            // Edited
    public class Person {
        protected String name; //setter getter also implemented
     } 
@XmlRootElement(name = "Animal")                             // Edited
    public class Animal {
       protected String name; //setter getter also implemented
     }


/* After Unmarshalling*/

/*解组后*/

 Home home ;

                for(Object obj : home .getAny()){
                    if(obj instanceof Person ){
                        Person  person = (Person )obj;
                        // .........
                    }else if(obj instanceof Animal ){
                        Animal animal = (Animal )obj;
                        // .........
                    }
                }

I need to achieve Person or Animalobject saved in "Home.any" Listvariable but content of "Home.any" Listis instance of com.sun.org.apache.xerces.internal.dom.ElementNSImplinstead of Animal or Person.

我需要实现Person or Animal保存在"Home.any" List变量中的对象,但内容"Home.any" List是实例com.sun.org.apache.xerces.internal.dom.ElementNSImpl而不是Animal or Person.

So is there a way to achieve Animal or Personinstance that is saved in xml in "Home.any" List.

那么有没有办法实现Animal or Person保存在 .xml 中的实例"Home.any" List

采纳答案by bdoughan

You need to add @XmlRootElementon the classes you want to appear as instances in the field/property you have annotated with @XmlAnyElement(lax=true).

您需要添加@XmlRootElement要在已使用 注释的字段/属性中显示为实例的类@XmlAnyElement(lax=true)

Java Model

Java模型

Home

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Home {
    @XmlAnyElement(lax = true)
    protected List<Object> any;

    //setter getter also implemented
}

Person

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Person")
public class Person {

}

}

Animal

动物

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Animal")
public class Animal {

}

Demo Code

演示代码

input.xml

输入文件

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <Person/>
    <Animal/>
    <Person/>
</root>

Demo

演示

import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Home.class, Person.class, Animal.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource xml = new StreamSource("src/forum20329510/input.xml");
        Home home = unmarshaller.unmarshal(xml, Home.class).getValue();

        for(Object object : home.any) {
            System.out.println(object.getClass());
        }
    }

}

Output

输出

class forum20329510.Person
class forum20329510.Animal
class forum20329510.Person

For More Information

想要查询更多的信息