dom4j读取xml文件 和 生成xml格式数据

此为转载地址不详侵权留言删chu

7c605b502ce6debd42f8ba1225920b807d1.jpg

选第二个

 

 

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值