JAXB中的对象

时间:2016-10-24 10:47:04

标签: java xml jaxb

我有一个XML,如下所示

<rootElement>
    <element1>data</element1>    
    <element2 attribute1="value1" attribute2="value2">
        <inner_element>2</inner_element>
    </element2>
</rootElement>

映射如下:

public class RootElement{
    @XmlElement(name = "element1")
    String elem1;
    @XmlElement(name = "element2")
    Element2 element2;
}

public class Element2{
    @XmlAttribute(name = "attribute1")
    String attr1;
    @XmlAttribute(name = "attribute2")
    String attr2;
    @XmlAnyElement(lax=true)
    Object value;    
}

我想允许用户在<inner_element>中输入任何类型的值(String,Integer,Double)。

在Java中,我能够阅读其他元素<element1>&amp; <element2>但是,我valuenull。不知道怎么办?

2 个答案:

答案 0 :(得分:1)

在InnerElement类上尝试使用@XmlValue而不是@XmlAnyElement。 也解析为String而不是Object

答案 1 :(得分:0)

使用此XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="rootElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element type="xs:string" name="element1"/>
                <xs:element name="element2">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:anyType" name="inner_element"/>
                        </xs:sequence>
                        <xs:attribute type="xs:string" name="attribute1"/>
                        <xs:attribute type="xs:string" name="attribute2"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

我生成这些类

package com.test.jaxb;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "element1",
    "element2"
})
@XmlRootElement(name = "rootElement")
public class RootElement {

    @XmlElement(required = true)
    protected String element1;
    @XmlElement(required = true)
    protected RootElement.Element2 element2;

    public String getElement1() {
        return element1;
    }

    public void setElement1(String value) {
        this.element1 = value;
    }

    public RootElement.Element2 getElement2() {
        return element2;
    }

    public void setElement2(RootElement.Element2 value) {
        this.element2 = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "innerElement"
    })
    public static class Element2 {

        @XmlElement(name = "inner_element", required = true)
        protected Object innerElement;
        @XmlAttribute(name = "attribute1")
        protected String attribute1;
        @XmlAttribute(name = "attribute2")
        protected String attribute2;

        public Object getInnerElement() {
            return innerElement;
        }

        public void setInnerElement(Object value) {
            this.innerElement = value;
        }

        public String getAttribute1() {
            return attribute1;
        }

        public void setAttribute1(String value) {
            this.attribute1 = value;
        }

        public String getAttribute2() {
            return attribute2;
        }

        public void setAttribute2(String value) {
            this.attribute2 = value;
        }

    }

}

这个ObjectFactory类

package com.test.jaxb;

import javax.xml.bind.annotation.XmlRegistry;

@XmlRegistry
public class ObjectFactory {

    public ObjectFactory() {
    }

    public RootElement createRootElement() {
        return new RootElement();
    }

    public RootElement.Element2 createRootElementElement2() {
        return new RootElement.Element2();
    }

}