将XML(具有相同标记名称)映射到Java Object [英] Map XML(of same tag name) to Java Object
本文介绍了将XML(具有相同标记名称)映射到Java Object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要求,我必须将我的xml映射到java对象而不解析它,但问题就像在xml标签名称中会相同,例如,
I got a requirement that I have to map my xml to java object without parsing it, but the problem is like that in xml tag names would be same, for example,
<response>
<employee>
<name>Sharique</name>
<name>24</name>
<name>India</name>
</employee>
</response>
和类就像这样
public class Employee{
private String empName;
private int age;
private String country;
//getters and setters
}
请帮忙! !
如果可以使用spring完成,那将是非常好的
Please help!! If it can be done using spring than that would be very nice
推荐答案
如果您利用EclipseLink MOXy作为您的JAXB (JSR-222)提供程序然后您可以使用我们的 @XmlPath
扩展名来处理此用例。
If you leverage EclipseLink MOXy as your JAXB (JSR-222) provider then you can use our @XmlPath
extension for this use case.
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee{
@XmlPath("name[1]/text()")
private String
@XmlPath("name[2]/text()")
private int age;
@XmlPath("name[3]/text()")
private String country;
//getters and setters
}
更多信息
我在博客上写了更多关于 @XmlPath
的扩展名:
I have written more about the @XmlPath
extension on my blog:
- http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
这篇关于将XML(具有相同标记名称)映射到Java Object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文