How to map xml fields without parent tag to java list?

白昼怎懂夜的黑 提交于 2021-02-11 06:14:47

问题


I have following xml response from web service which need to map to java class.

<api1Response>
   <timeStamp>06/24/2016 07:45:10</timeStamp>
   <ippProjectName>xyz</ippProjectName>
   <project>
      <projectId>1</projectId>
      <projectName>ABC</projectName>
      <dataDate>06/06/2016</dataDate>
   </project>
   <project>
      <projectId>2</projectId>
      <projectName>PQR</projectName>
      <dataDate>06/06/2016</dataDate>
   </project>
</api1Response>   

I have mapping class like

class api1Response{
    String timeStamp;
    String ippProjectName;
    //how to map project
}

As project tags not having any parent element how i can map it to List using jaxb. I know it will be easy if "project" tags having parent elements like "projects" but response is not in my control and i still need to parse it with jaxb.


回答1:


Just using XmlElement over a list of projects shoud do the trick :

class api1Response{
    String timeStamp;
    String ippProjectName;
    @XmlElement(name="project")
    List<Project> projects;
}


来源:https://stackoverflow.com/questions/38008261/how-to-map-xml-fields-without-parent-tag-to-java-list

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