尝试将我的类映射到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: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
复制相似问题