首页 > 解决方案 > 如何使用 java 注释读取 xsi:type

问题描述

我想将基于 jaxb 的 xml 文件读入我的面向对象结构。

可以说这是我的 xml 文件:

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <children xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <child xsi:type="girl">
            <age>12</age>
            <isdancing>true</isdancing>
        </child>
        <child xsi:type="boy">
            <age>10</age>
            <issoccerplayer>true</issoccerplayer>
        </child>
    </children>

children是某种包装元素,包括多个元素。孩子可以是由 xsi:type 指定的男孩或女孩。这两个类有一些共同的元素(如age)和一些不同的(排除)元素(如isdancingissoccerplayer

要读取文件,我有这个方法:

    public static void main( String[] args ) throws JAXBException
    {
        JAXBContext jaxbContext;
        jaxbContext = JAXBContext.newInstance(Children.class);             
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        File file = new File("C:/test.xml");
        if (!file.exists()) System.out.println("File does not exist");

        Children children = (Children) jaxbUnmarshaller.unmarshal(file);
        System.out.println(children.toString());
    }

我的儿童课程如下所示:

    @XmlRootElement(name="children")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Children {

        @XmlElement(name="child")
        private List<Child> childrenList;

        public List<Child> getChildren() { return childrenList; }
        public void setChildren(List<Child> children) {this.childrenList = children;}

    @Override
        public String toString() {
            return ReflectionToStringBuilder.toString(this);
        }
    }

我的孩子类看起来像这样:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Child {

    @XmlAttribute(name="xsi:type")
    private XsiType xsiType;

    private int age;

    @XmlElement(name = "isdancing")
    private boolean isDancing;

    @XmlElement(name = "issoccerplayer")
    private boolean isSoccerPlayer;

        //Getter and setter for all fields

    @Override
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
        }
    }

我的 XsiType 类看起来像这样:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class XsiType {

        @XmlAttribute(name="xsi:type")
        private String name;

        @XmlValue
        private String value;

        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getValue() { return value; 
        public void setValue(String value) { this.value = value; }
    }

在我的 pom.xml 中,我包含了以下依赖项:

    <dependencies>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>

我现在的问题是,输出没问题,但是 Child-class 的元素 xsiType 始终为 null,否则最终会出现与 XmlTest.model.Child.xsiType 相关的 IllegalAnnotationExceptions

所以我希望设置任何类型的@Xml-Annotation 都会出错。有人可以通过找出错误来帮助我吗?

目标是迭代子列表并在运行时(基于 xsiType)决定这是女孩还是男孩。

谢谢

标签: javaxmljaxbxml-namespaces

解决方案


你不需要你的XsiType课。你可以String改用。

在您的Child班级中,该xsiType属性应如下所示。

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

注意:在@XmlAttribute注释中

  • 使用name = "type"(不带前缀xsi:
  • 指定namespaceXML 中给定的参数xmlns:xsi="..."

顺便说一句: 最好使用常量
,而不是键入字符串。所以你改进的代码会是这样的:"http://www.w3.org/2001/XMLSchema-instance"XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI

@XmlAttribute(name = "type", namespace = XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)
private String xsiType;

推荐阅读