如何使用JAXB解组列表元素

时间:2015-11-03 09:50:31

标签: java jaxb

在阅读列表EmployeeParamDataEmployeeParam

的attirubutes和值方面需要帮助

我的XML

    <HostedEmployee>
        <employeeid>12345</employeeid>
        <employeeage>26</employeeage>
        <employeeParamData>
            <employeeParam name="attribute1"/>
            <employeeParam name="attribute2">attribute2_value</employeeParam>
            <employeeParam name="attribute3">attribute3_value</employeeParam>
            <employeeParam name="attribute4">attribute4_value</employeeParam>
            <employeeParam name="attribute5"/>
        </employeeParamData>
    </HostedEmployee>

我的域类是:

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="HostedEmployee")
public class HostedEmployee {

    private String employeeId;      

    public String getEmployeeId() {
        return employeeId;
    }

    @XmlElement
    public void setEmployeeId(String employeeId) {
        this.employeeId= employeeId;
    }
}

解组在这里完成

public static void main(String[] args) {

        try {

            File file = new File(".../jaxbtest.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(HostedEmployee.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            HostedEmployee hostedEmployee= (HostedEmployee) jaxbUnmarshaller.unmarshal(file);
            System.out.println(hostedEmployee.getEventGuid());

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }

0 个答案:

没有答案