此为转载地址不详侵权留言删chuweb
选第二个app
读取xml dom
@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格式webapp
@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()); } }