对xml的处理_Dom4j入门五

以下介绍Dom4j的环境是:windowsXP、eclipse3.5.0、jdk1.6、Dom4j1.6.1.jar

Dom4j1.6.1.jar架包可以在本人的资源中下载,欢迎下载使用

xml的合并问题,确保xml的结构相同

 //合并2个xml
 public static void getMergerXml(String folderxmlpath,String xmlresultpath){
  
           createDoucument_01(xmlresultpath);//创建合并后的xml
           SAXReader readerxml=new SAXReader(); 
           File[] file = new File(folderxmlpath).listFiles();//获取所有的xml文件
  
           try {
                     Document docresult=readerxml.read(new File(xmlresultpath));//将合并后的xml文件转成document对象
                     Element elementresult=docresult.getRootElement();//获取document对象的根节点
                     for(int i = 0;i<file.length;i++){
                              Document docfile = readerxml.read(file[i]);//将要合并的xml对象包装成一个document对象
                              Element elementfile=docfile.getRootElement();//获取要合并的document对象的跟节点
                             elementresult.add(elementfile);//给合并后的xml对象中添加要合并的对象
                      }
                      WriteInFiler(docresult,xmlresultpath);//将合并后的document对象写入xml中
            } catch (DocumentException e) {
                      e.printStackTrace();
            }
 }

 //将folderxmlpath文件夹下的所有xml合并
 public static void getMergerAllXml(String folderxmlpath,String xmlresultpath){
          SAXReader readerxml=new SAXReader();
          createDoucument_01(xmlresultpath);//创建合并后的xml
          List xmlpath = recursion_Directory(new File(folderxmlpath),new ArrayList());//获取该目录下所有的xml文件绝对路径
  
          try {
                    Document docresult=readerxml.read(new File(xmlresultpath));//将合并后的xml文件转成document对象
                    Element elementresult=docresult.getRootElement();//获取document对象的根节点
                    for(int i = 0;i<xmlpath.size();i++){
                             Document docfile = readerxml.read(new File(xmlpath.get(i).toString()));//将要合并的xml对象包装成一个document对象
                             Element elementfile=docfile.getRootElement();//获取要合并的document对象的跟节点
                             elementresult.add(elementfile);//给合并后的xml对象中添加要合并的对象
                     }
                    WriteInFiler(docresult,xmlresultpath);//将合并后的document对象写入xml中
          } catch (DocumentException e) {
                   e.printStackTrace();
          } 
 }
 //将document对象内容写入文件doc.xml文件中
 public static void WriteInFiler(Document doc,String xmlpath){
           try{
                   OutputFormat opt=OutputFormat.createPrettyPrint();
                   opt.setEncoding("GB2312");
                   XMLWriter w=new XMLWriter(new FileWriter(new File(xmlpath)),opt);
                   w.write(doc);
                   w.close();
            }catch(Exception e){
                  e.printStackTrace();
           }
 }
 
 //创建合并后的xml文件
 public static void createDoucument_01(String xmlpath){
            //创建xml文档,并设置文档根元素
            Document document =DocumentHelper.createDocument();
            Element element1 = DocumentHelper.createElement("root");
            document.setRootElement(element1);//快捷键:ctrl+shift+o  
            //输出到指定文件,设置其字符编码和输出格式
            OutputFormat format=new OutputFormat(" ",true);
             format.setEncoding("GBK");//设置xml的字符编码格式
             try{
                     XMLWriter xml3=new XMLWriter(new FileOutputStream(xmlpath),format);
                     xml3.write(document);
                     xml3.close();
             }catch(IOException se){
                    se.printStackTrace();
             } 
 }
 //递归获取file下的所有xml文件绝对路径
 public static List recursion_Directory(File file,List fileList){
         File[] files=file.listFiles();
          if(files!=null&&files.length>0){
                  for(File filechild : files){
                               if(filechild.isDirectory()){//如果是文件夹
                                          recursion_Directory(filechild,fileList);//递归查询
                               }else{
                                          String filepath = filechild.getPath();
                                          String suffix = filepath.substring(filepath.lastIndexOf(".")); 
                                          if(suffix.equals(".xml")){
                                                     fileList.add(filepath);
                                           }
                                 }
                      }
           }
           return fileList;
 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章