简体   繁体   English

我想通过解组将XML转换为Java

[英]I want to convert XML to Java by unmarshalling

This is my XML, and I want to unmarshall it. 这是我的XML,我想解组。

<?xml version="1.0" encoding="UTF-8"?>
<departments>
    <deptname name="Research">
        <employee>
            <eid>r-001</eid>
            <ename>Dinesh R</ename>
            <age>35</age>
            <deptcode>d1</deptcode>
            <deptname>Research</deptname>
            <salary>20000</salary>
        </employee>
    </deptname>
    <deptname name="Sales">
        <employee>
            <eid>s-001</eid>
            <ename>Kanmani S</ename>
            <age>35</age>
            <deptcode>d2</deptcode>
            <deptname>Sales</deptname>
            <salary>30000</salary>
        </employee>
    </deptname>
</departments>

Department.java 部门.java

public class Department 
{ 
    @XmlAttribute(name = "deptname")
    private String name;

    @XmlElement(name = "employee") 
    private List<Employee> employee = new ArrayList<>();//getter and setter//
}

This is my Departments.java 这是我的Departments.java

public class Departments {

    List<Department> deptname;

    public List<Department> getDeptname() {
        return deptname;
    }

    public void setDeptname(List<Department> deptname) {
        this.deptname = deptname;
    }
}

This is my Unmarshalling.java 这是我的Unmarshalling.java

public class Unmarshalling {
    public void testXML() {
        try {
            File file = new File(
                "/home/scrunch/work/workspace/sts/default/EmployeeUnmarshall/src/main/java/OutputXml.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Departments.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Departments departments = (Departments) jaxbUnmarshaller.unmarshal(file);
            System.out.println(departments);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

I have tried unmarshalling but i didn't get the java object .I got xml format. 我曾尝试解组,但没有得到java对象。我得到了xml格式。 I am doing the Rest service. 我在做休息服务。 For that, I need to unmarshall the XML file, so then I could get the Java objects.Could you please update me sir. 为此,我需要解组XML文件,然后才能获取Java对象。先生,请给我更新一下。

I tried your code and ended up having this error: 我尝试了您的代码,并最终出现此错误:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"departments"). 线程“主”中的异常javax.xml.bind.UnmarshalException:意外元素(uri:“”,local:“部门”)。 Expected elements are (none) 预期元素为(无)

Fixed it by declaring Departments this way: 通过这样声明Departments此问题:

@XmlRootElement(name="departments")
public class Departments {

    List<Department> deptname;

    public List<Department> getDeptname() {
        return deptname;
    }

    public void setDeptname(List<Department> deptname) {
        this.deptname = deptname;
    }
}

See that question 看到那个问题

Have a try 试试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM