Java 具有自定义属性的jaxb xmlelement编组

Java 具有自定义属性的jaxb xmlelement编组,java,jaxb,Java,Jaxb,尝试将我的类映射到xml并添加自定义属性 public class MyXmlMappings { @XmlElement protected String username; @XmlElement protected String password; @XmlElement protected Integer age; } 编组为xml后,如下所示: <myXmlMappings> <username/> <p

尝试将我的类映射到xml并添加自定义属性

public class MyXmlMappings  {
    @XmlElement
    protected String username;
    @XmlElement
    protected String password;
    @XmlElement
    protected Integer age;
}
编组为xml后,如下所示:

<myXmlMappings>
<username/>
<password/>
<age/>
</myXmlMappings>
<myXmlMappings>
<username type="String" defaultValue="hello" />
<password type="String" defaultValue="asdf" />
<age type="Integer" defaultValue="25" />
</myXmlMappings>

我需要这样的xml:

<myXmlMappings>
<username/>
<password/>
<age/>
</myXmlMappings>
<myXmlMappings>
<username type="String" defaultValue="hello" />
<password type="String" defaultValue="asdf" />
<age type="Integer" defaultValue="25" />
</myXmlMappings>

如您所见,我添加了type和defaultValue属性。如何将它们添加到myXmlMappings类中,以便在编组后可见


向myXmlMappings类添加额外字段是不可行的,我希望通过注释以某种方式进行添加。

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>
试试这个:


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

+1-此解决方案利用EclipseLink JAXB(MOXy)中的
@XmlPath
扩展:默认值从何而来?