JAXB解组为具有属性和文本内容的复杂类型

时间:2016-02-23 14:24:16

标签: java xml jaxb

我一直在尝试使用JAXB解组以下XML内容。

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://wso2.org/2016/wso2as-web">
    <Property Key="name">value</Property>
</Root> 

在几篇文章中提到在这种情况下使用@XmlValue注释来检索文本内容,但到目前为止由于以下问题我失败了。

If a class has @XmlElement property, it cannot have @XmlValue property

我到目前为止准备的代码如下:

package org.test;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
    private Property property;

    public Property getPropertyObject() {
        return property;
    }

    public void setPropertyObject(Property property) {
        this.property = property;
    }

    @XmlRootElement(name = "Property")
    public static class Property {
        @XmlAttribute(name = "Key")
        private String key;
        @XmlValue
        private String text;

        public String getKeyObject() {
            return key;
        }

        public void setKeyObject(String key) {
            this.key = key;
        }

        public String getValueObject() {
            return text;
        }

        public void setValueObject(String value) {
            this.text = value;
        }
    }
}

任何有关此方面的帮助都非常受欢迎,因为我对JAXB相对较新。

3 个答案:

答案 0 :(得分:3)

您必须使用Property@XmlAccessorType(XmlAccessType.FIELD)课程添加注释。

否则,其getXxx()方法被视为元素,因为getter的名称与字段的名称不匹配。

答案 1 :(得分:1)

想要添加到Benjamin的答案中,您的异常是由于内部类Property没有注释 @XmlAccessorType(XmlAccessType.NONE)@XmlAccessorType(XmlAccessType.FIELD)

答案 2 :(得分:0)

我找到了上述案例的答案。以上两个答案确实解决了弹出的异常问题(在帖子中提到),但没有加载任何与加载无关的内容。

这是固定代码:

package org.test;

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.XmlValue;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Root")
public class Root {
    @XmlElement(name = "Property")
    private Property property;

    public Property getPropertyObject() {
        return property;
    }

    public void setPropertyObject(Property property) {
        this.property = property;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Property {
        @XmlAttribute(name = "Key")
        private String key;
        @XmlValue
        private String text;

        public String getKeyObject() {
            return key;
        }

        public void setKeyObject(String key) {
            this.key = key;
        }

        public String getValueObject() {
            return text;
        }

        public void setValueObject(String value) {
            this.text = value;
        }
    }
}

我从嵌套的Property类中删除了@XmlRootElement(name =“Property”)注释,并将@XmlElement(name =“Property”)添加到Root.java的Property实例变量中。