我有以下 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'属性和内容,那么我做错了什么?