简体   繁体   English

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

[英]JAXB unmarshalling for complex type with attributes and text content

I have been attempting to unmarshall the following XML content using JAXB. 我一直在尝试使用JAXB解组以下XML内容。

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

It was mentioned in several posts to use the @XmlValue annotation in such a case to retrieve the text content but so far due to the following issue I have failed. 在几篇文章中提到在这种情况下使用@XmlValue批注来检索文本内容,但到目前为止,由于以下问题,我未能通过。

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

The code I have prepared so far is follows: 到目前为止,我准备的代码如下:

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;
        }
    }
}

Any help with regards to this highly appreciated as I am relatively new to JAXB. 由于我是JAXB的新手,因此对此方面的任何帮助均受到高度赞赏。

You must annotate the Property class with @XmlAccessorType(XmlAccessType.FIELD) . 您必须使用@XmlAccessorType(XmlAccessType.FIELD)注释Property类。

Else, its getXxx() methods are considered as elements since the name of the getters do not match the names of the fields. 另外,由于getter的名称与字段的名称不匹配,因此将其getXxx()方法视为元素。

要添加到本杰明的答案中,您的例外是由于内部类Property没有注释@XmlAccessorType(XmlAccessType.NONE)@XmlAccessorType(XmlAccessType.FIELD)

I found the answer for the above case. 我找到了上述情况的答案。 The above two answers do solve the exception issue that pops up (mentioned in the post) but does not load anything related to the loading which does not work out properly. 上面的两个答案确实解决了弹出的异常问题(在帖子中提到),但是没有加载与无法正常工作的加载相关的任何内容。

Here is the fixed code: 这是固定代码:

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;
        }
    }
}

I removed the @XmlRootElement(name = "Property") annotation from the nested Property class and added @XmlElement(name = "Property") to the Property instance variable of Root.java. 我从嵌套的Property类中删除了@XmlRootElement(name =“ Property”)批注,并将@XmlElement(name =“ Property”)添加到Root.java的Property实例变量中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM