此为转载地址不详侵权留言删chu
选第二个
读取xml
@Test
public DoctorRequest findDoctorMessage(String filePath) throws DocumentException {
DoctorRequest re = new DoctorRequest();
SAXReader reader = new SAXReader();
//如果传的是一个xml文件,这个filePath就是文件地址
File file = new File("E:\\ssm3\\src\\main\\webapp\\WEB-INF\\book.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
re.setServiceId(child.elementText("serviceId"));
re.setSysId(child.elementText("sysId"));
re.setOperator(child.elementText("operator"));
}
//未知子元素名情况下
/*List<Element> elementList = child.elements();
for (Element ele : elementList) {
System.out.println(ele.getName() + ": " + ele.getText());
}
System.out.println();*/
return re;
}
生成xml格式
@RequestMapping("/findUser")
public void findDoctorMessage(DoctorRequest request) throws DocumentException {
Document document;
SAXReader reader = new SAXReader();
File file = new File("E:/ssm3/src/main/webapp/WEB-INF/book.xml");
document = reader.read(file);
Element root = document.getRootElement();
List<Element> childElements = root.elements();
for (Element child : childElements) {
//未知属性名情况下
List<DoctorRequest> attributeList = child.attributes();
for (DoctorRequest attr : attributeList) {
System.out.println(attr.getSysId() + ": " + attr.getOperator());
DoctorEmpInfo doctorEmpInfo = new DoctorEmpInfo();
doctorEmpInfo.setEmpId(001);
//注:这里生成xml的方式是用dom4j这个类来生成的
//创建最外面一层标签
Element root1 = DocumentHelper.createElement("empInfos");
document = DocumentHelper.createDocument(root1);
//创建第二层标签
Element itemNo= root.addElement("empInfo");
//创建第三层标签
Element itemNo1 = itemNo.addElement("empId");
//放值
itemNo1.setText(doctorEmpInfo.getEmpId()+"");
//...........就这样一层一层的封装
System.out.println(document.asXML());
}
}
转载于:https://my.oschina.net/u/3774949/blog/3012500