通过使用 JAXB 通过取消编组将 XML 转换为对象

我想通过使用JAXB取消编组将XML转换为java。


这是我的员工.xml。我试图转换,但它显示错误javax.xml.bind.UnmarshalException


<?xml version="1.0" encoding="UTF-8"?>

<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>

<deptname name="Delivery">

  <employee>

    <eid>d-001</eid>

    <ename>Kanchana M</ename>

    <age>35</age>

    <deptcode>d3</deptcode>

    <deptname>Delivery</deptname>

    <salary>20000</salary>

</employee>

  </deptname>


红颜莎娜
浏览 65回答 1
1回答

www说

根据 XML 文档,您只能有一个根元素,因此应将 xml 更改为:<?xml version="1.0" encoding="UTF-8"?><departments>&nbsp; &nbsp; <deptname name="Research">&nbsp; &nbsp; &nbsp; &nbsp; <employee>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <eid>r-001</eid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ename>Dinesh R</ename>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <age>35</age>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptcode>d1</deptcode>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptname>Research</deptname>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <salary>20000</salary>&nbsp; &nbsp; &nbsp; &nbsp; </employee>&nbsp; &nbsp; </deptname>&nbsp; &nbsp; <deptname name="Sales">&nbsp; &nbsp; &nbsp; &nbsp; <employee>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <eid>s-001</eid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ename>Kanmani S</ename>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <age>35</age>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptcode>d2</deptcode>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptname>Sales</deptname>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <salary>30000</salary>&nbsp; &nbsp; &nbsp; &nbsp; </employee>&nbsp; &nbsp; </deptname>&nbsp; &nbsp; <deptname name="Delivery">&nbsp; &nbsp; &nbsp; &nbsp; <employee>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <eid>d-001</eid>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ename>Kanchana M</ename>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <age>35</age>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptcode>d3</deptcode>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <deptname>Delivery</deptname>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <salary>20000</salary>&nbsp; &nbsp; &nbsp; &nbsp; </employee>&nbsp; &nbsp; </deptname></departments>然后,您需要一个类部门来阅读该文章:@XmlRootElement(name="departments")public class Departments {&nbsp; List<Department> deptname;&nbsp; // Getters and setters}测试用例是:@Test&nbsp; public void testXML() {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; File file = new File("./src/main/resources/employee.xml");&nbsp; &nbsp; &nbsp; JAXBContext jaxbContext = JAXBContext.newInstance(Departments.class);&nbsp; &nbsp; &nbsp; Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();&nbsp; &nbsp; &nbsp; Departments departments = (Departments) jaxbUnmarshaller.unmarshal(file);&nbsp; &nbsp; &nbsp; System.out.println(departments);&nbsp; &nbsp; } catch (JAXBException e) {&nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }&nbsp; }其次,您没有“研究”或“销售”或“交付”属性,您有一个可以采用任何字符串值的属性“name”,因此您需要更改实现:public class Department {&nbsp; @XmlAttribute(name = "name")&nbsp; private String name;&nbsp; @XmlElement(name = "employee")&nbsp; private List<Employee> employee = new ArrayList<>();&nbsp; // Getters and setters}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java