通过使用 JAXB 解组 XML 到对象

我想通过使用 JAXB 解组将 XML 转换为 java。


这是我的employee.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>


梦里花落0921
浏览 80回答 1
1回答

繁星coding

根据 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; }其次,您没有“Research”或“Sales”或“Delivery”属性,您有一个可以采用任何字符串值的属性“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