尝试将我的类映射到xml并添加自定义属性。
public class MyXmlMappings {
@XmlElement
protected String username;
@XmlElement
protected String password;
@XmlElement
protected Integer age;
}在编组到xml之后,看起来像这样:
<myXmlMappings>
<username/>
<password/>
<age/>
</myXmlMappings>我需要这样的xml:
<myXmlMappings>
<username type="String" defaultValue="hello" />
<password type="String" defaultValue="asdf" />
<age type="Integer" defaultValue="25" />
</myXmlMappings>如您所见,我添加了类型和defaultValue属性。如何将它们添加到myXmlMappings类中,使其在编组后可见?
添加额外的字段到myXmlMappings类是不可行的,我想以某种方式用注释来做到这一点。
发布于 2012-08-01 23:44:40
XML表示法
我建议使用以下XML表示法:
<myXmlMappings>
<xmlMapping name="username" type="String" defaultValue="hello" />
<xmlMapping name="password" type="String" defaultValue="asdf" />
<xmlMapping name="age" type="Integer" defaultValue="25" />
</myXmlMappings>Java模型
使用以下Java模型:
XmlMappings
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyXmlMappings {
@XmlElement(name="xmlMapping")
protected List<XmlMapping> xmlMappings;
}XmlMapping
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlMapping {
@XmlAttribute
protected String name;
@XmlAttribute
protected String type;
@XmlAttribute
protected String defaultValue;
}发布于 2012-08-01 23:45:53
试试这个:
public class MyXmlMappings {
@XmlPath("username/@type")
protected String userType;
@XmlPath("password/@type")
protected String passwordType;
@XmlPath("age/@type")
protected String ageType;
@XmlPath("username/@defaultValue")
protected String userDefaultValue;
@XmlPath("password/@defaultValue")
protected String passwordDefaultValue;
@XmlPath("age/@defaultValue")
protected Integer ageDefaultValue;
@XmlElement
protected String username;
@XmlElement
protected String password;
@XmlElement
protected Integer age;
}https://stackoverflow.com/questions/11762271
复制相似问题