gpt4 book ai didi

java - 嵌套元素列表的 JAXB 注释

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:26 25 4
gpt4 key购买 nike

我有以下 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 类:

@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”属性和内容,那么我做错了什么?

最佳答案

默认 JAXB (JSR-222)实现将公共(public)属性(获取/设置方法)和带注释的字段视为映射(和分离)。默认映射为 @XmlElement,因此您的属性将被视为以这种方式映射。

解决方案#1

由于您正在注释字段,因此您需要在类中添加 @XmlAccessorType(XmlAccessType.FIELD)

@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)方法。

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

了解更多信息


更新

您还需要在 mappings 属性上使用 @XmlElement 注释来指定元素名称应该是 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;
}
}

关于java - 嵌套元素列表的 JAXB 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14202529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com