Java/JAXB: Unmarshall XML elements with same name but different attribute values to different class members

非 Y 不嫁゛ 提交于 2020-01-12 09:29:11

问题


I am trying to parse XML that has several "Fields" elements to different class members according to one of their attributes. Here is the XML:

<Series>
    <Fields type="SelectedFields" operation="SUM">
        <Field name="Remaining" />
        <Field name="Invested" />
    </Field>
    <Fields type="FirstSelectedFields" operation="SUM">
        <Field name="Estimated" />
    </Field>
</Series>

And here is the java class it should be mapped to:

public class APMSeries {

    private List<Field> selectedFields;

    private List<Field> firstSelectedFields;

}

Can anyone tell me how can I set the Fields element with attribute type="SelectedFields" to the selectedFields member and the Fields element with the attribute type="FirstSelectedFields" to the firstSelectedFields member?


回答1:


public class APMSeries {

    @XmlElementWrapper(name="SelectedFields")
    private List<Field> selectedFields;

    @XmlElementWrapper(name="FirstSelectedFields")
    private List<Field> firstSelectedFields;

}


来源:https://stackoverflow.com/questions/5078479/java-jaxb-unmarshall-xml-elements-with-same-name-but-different-attribute-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!