使用itemReader Spring Batch读取XML中节点的属性

时间:2016-06-20 18:38:47

标签: java xml spring-batch

我的xml低于

<?xml version="1.0" encoding="UTF-8"?>
<employee siteId="1">
   <name>john</name>
   <age>36</age>
</employee>

我想使用siteId获取itemReader(属性值)的值。我能够成功阅读nameage子元素。我不知道如何获得siteId的价值。请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以通过在课堂上指定@XmlAttribute(name =&#34; siteId&#34;)来读取siteId属性值,请在下面找到示例

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "employee")
public class Employee {

    private int siteId;
    private String name;
    private int age;


    @XmlAttribute(name = "siteId")
    public int getSiteId() {
        return siteId;
    }

    public void setSiteId(int siteId) {
        this.siteId= siteId;
    }

    @XmlElement(name = "age")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}