嵌套元素列表的JAXB注释 [英] JAXB annotations for nested element lists

查看:714
本文介绍了嵌套元素列表的JAXB注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML:

<mappings>
    <mapping>
        <parameter attr = "value">asdas</parameter>
        <parameter attr = "value2">d123asdsad</parameter>
        <parameter attr = "value3">0</parameter>
    </mapping>
    <mapping>
        <parameter attr = "value">23123s</parameter>
        <parameter attr = "value2">qwerty</parameter>
        <!-- more parameter elements -->
    </mapping>
    <!-- more mapping elements -->
</mappings>

我有以下java类来映射它:

I have the following java classes to map it to:

@XmlRootElement(name = "mappings")
public class Mappings { 
    @XmlElement(name = "mapping")
    private List<Mapping> mMappings;

    public List<Mapping> getMappings() {
        return mMappings;
    }

    public void setMappings(List<Mapping> aMappings) {
        this.mMappings = aMappings;
    }
}

public class Mapping {
    @XmlElement(name = "parameter")
    private List<Parameter> mParameters;

    public List<Parameter> getParameters() {
        return mParameters;
    }

    public void setParameters(List<Parameter> aParameters) {
        this.mParameters = aParameters;
    }
}

public class Parameter {
    @XmlAttribute(name = "attr")
    private String mName;

    @XmlValue
    private String mValue;

    public String getName() {
        return mName;
    }

    public void setName(String aName) {
        this.mName = aName;
    }

    public String getValue() {
        return mValue;
    }

    public void setValue(String aValue) {
        this.mValue = aValue;
    }
}

当我尝试用

JAXBContext context = JAXBContext.newInstance(BundleMappings.class);
Unmarshaller um = context.createUnmarshaller();
mappings = (BundleMappings)um.unmarshal(new File(myFile));

我收到此错误

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

我需要参数同时具有'attr'属性和内容,所以我做错了什么?

I need parameter to have both the 'attr' attribute and content, so what am I doing wrong?

推荐答案

默认 JAXB(JSR-222) 实现将公共属性(get / set方法)和带注释的字段视为映射(和单独)。默认映射是 @XmlElement ,因此您的属性将被视为以这种方式映射。

By default JAXB (JSR-222) implementations consider public properties (get/set methods) and annotated fields as mapped (and separate). The default mapping is @XmlElement so your properties will be considered as mapped this way.

解决方案# 1

由于您要注释字段,因此需要添加 @XmlAccessorType(XmlAccessType.FIELD)在你的课程上。

Since you are annotating the fields you need to add @XmlAccessorType(XmlAccessType.FIELD) on your classes.

@XmlAccessorType(XmlAccessType.FIELD)
public class Parameter {
    @XmlAttribute(name = "attr")
    private String mName;

    @XmlValue
    private String mValue;

    public String getName() {
        return mName;
    }

    public void setName(String aName) {
        this.mName = aName;
    }

    public String getValue() {
        return mValue;
    }

    public void setValue(String aValue) {
        this.mValue = aValue;
    }
}

解决方案#2

注释get(或set)方法。

Annotate the get (or set) methods.

public class Parameter {
    private String mName;

     private String mValue;

    @XmlAttribute(name = "attr")
    public String getName() {
        return mName;
    }

    public void setName(String aName) {
        this.mName = aName;
    }

    @XmlValue
    public String getValue() {
        return mValue;
    }

    public void setValue(String aValue) {
        this.mValue = aValue;
    }
}

更多信息

  • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
  • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html

更新

您还需要在<$ c $上使用 @XmlElenent 注释c> mappings 指定元素名称的属性应为 mapping

You will also need to use the @XmlElenent annotation on the mappings property to specify the element name should be mapping.

@XmlRootElement(name = "mappings")
public class Mappings { 
    private List<Mapping> mMappings;

    @XmlElement(name="mapping")
    public List<Mapping> getMappings() {
        return mMappings;
    }

    public void setMappings(List<Mapping> aMappings) {
        this.mMappings = aMappings;
    }
}

这篇关于嵌套元素列表的JAXB注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆