简体   繁体   中英

JAXB unmarshalling for complex type with attributes and text content

I have been attempting to unmarshall the following XML content using JAXB.

<?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.

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.

You must annotate the Property class with @XmlAccessorType(XmlAccessType.FIELD) .

Else, its getXxx() methods are considered as elements since the name of the getters do not match the names of the fields.

要添加到本杰明的答案中,您的例外是由于内部类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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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