我想通过解组将 XML 转换为 Java
I want to convert XML to Java by unmarshalling
这是我的 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
public class Department
{
@XmlAttribute(name = "deptname")
private String name;
@XmlElement(name = "employee")
private List<Employee> employee = new ArrayList<>();//getter and setter//
}
这是我的Departments.java
public class Departments {
List<Department> deptname;
public List<Department> getDeptname() {
return deptname;
}
public void setDeptname(List<Department> deptname) {
this.deptname = deptname;
}
}
这是我的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();
}
}
}
我试过解组,但没有得到 java 对象。我得到的是 xml 格式。我正在做休息服务。为此,我需要解组
XML 文件,这样我就可以得到 Java objects.Could 先生,请您更新我。
我试过你的代码,结果出现了这个错误:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"departments"). Expected elements are (none)
通过这样声明 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;
}
}
参见 that question
试一试
这是我的 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
public class Department
{
@XmlAttribute(name = "deptname")
private String name;
@XmlElement(name = "employee")
private List<Employee> employee = new ArrayList<>();//getter and setter//
}
这是我的Departments.java
public class Departments {
List<Department> deptname;
public List<Department> getDeptname() {
return deptname;
}
public void setDeptname(List<Department> deptname) {
this.deptname = deptname;
}
}
这是我的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();
}
}
}
我试过解组,但没有得到 java 对象。我得到的是 xml 格式。我正在做休息服务。为此,我需要解组 XML 文件,这样我就可以得到 Java objects.Could 先生,请您更新我。
我试过你的代码,结果出现了这个错误:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"departments"). Expected elements are (none)
通过这样声明 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;
}
}
参见 that question
试一试