I have the following XML:
我有以下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>
I have the following java classes to map it to:
我有以下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;
}
}
When I try to unmarshall it with
当我试图解开它的时候
JAXBContext context = JAXBContext.newInstance(BundleMappings.class);
Unmarshaller um = context.createUnmarshaller();
mappings = (BundleMappings)um.unmarshal(new File(myFile));
I get this error
我得到这个错误
If a class has @XmlElement property, it cannot have @XmlValue property.
I need parameter to have both the 'attr' attribute and content, so what am I doing wrong?
我需要参数来拥有“attr”属性和内容,那么我做错了什么呢?
1 个解决方案
#1
14
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.
默认情况下,JAXB (JSR-222)实现考虑公共属性(get/set方法)和注释字段作为映射(和独立)。默认映射是@XmlElement,因此您的属性将被认为是这样映射的。
Solution #1
解决方案1
Since you are annotating the fields you need to add @XmlAccessorType(XmlAccessType.FIELD)
on your classes.
由于您正在注释需要在类上添加@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;
}
}
Solution #2
解决方案2
Annotate the get (or set) methods.
注释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;
}
}
For More Information
的更多信息
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
UPDATE
更新
You will also need to use the @XmlElenent
annotation on the mappings
property to specify the element name should be mapping
.
您还需要在mappings属性上使用@XmlElenent注释来指定应该映射的元素名。
@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;
}
}
#1
14
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.
默认情况下,JAXB (JSR-222)实现考虑公共属性(get/set方法)和注释字段作为映射(和独立)。默认映射是@XmlElement,因此您的属性将被认为是这样映射的。
Solution #1
解决方案1
Since you are annotating the fields you need to add @XmlAccessorType(XmlAccessType.FIELD)
on your classes.
由于您正在注释需要在类上添加@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;
}
}
Solution #2
解决方案2
Annotate the get (or set) methods.
注释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;
}
}
For More Information
的更多信息
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
- http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
UPDATE
更新
You will also need to use the @XmlElenent
annotation on the mappings
property to specify the element name should be mapping
.
您还需要在mappings属性上使用@XmlElenent注释来指定应该映射的元素名。
@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;
}
}