• 用dom4j修改xml(增加修改节点)


    使用dom4j修改解析xml,xml文件的位置是配置在xml.properties文件中,程序打成jar包,在命令行中执行如下命令即可:

    [img]http://dl.iteye.com/upload/attachment/150593/6b4ac972-cf45-3582-9a04-5e88bd0c962d.jpg" alt="[/img]

    主要的java代码如下:

    Java代码  收藏代码
    1. import java.io.BufferedInputStream;  
    2. import java.io.File;  
    3. import java.io.FileInputStream;  
    4. import java.io.FileOutputStream;  
    5. import java.io.IOException;  
    6. import java.io.InputStream;  
    7. import java.util.Iterator;  
    8. import java.util.List;  
    9. import java.util.Properties;  
    10.   
    11. import javax.servlet.http.HttpServlet;  
    12.   
    13. import org.dom4j.Document;  
    14. import org.dom4j.DocumentException;  
    15. import org.dom4j.Element;  
    16. import org.dom4j.Node;  
    17. import org.dom4j.io.SAXReader;  
    18. import org.dom4j.io.XMLWriter;  
    19.   
    20.   
    21. public class RefreshXml extends HttpServlet {  
    22.     private String xmlFilePath="";  
    23.       
    24.     public void modifyXml(String xmlProperty) throws DocumentException, IOException {  
    25.         this.getProperties(xmlProperty);  
    26.         File file = new File(xmlFilePath);  
    27.         SAXReader reader = new SAXReader();  
    28.         Document doc = reader.read(file);  
    29.         List list;  
    30.   
    31.         /* 
    32.          * 修改EntityProps 
    33.          */  
    34.         list = doc.selectNodes("//Entity_Groups//Entity//EntityProps");  
    35.         System.out.println("需要修改的EntityProps一共有:"+list.size()+"处。");  
    36.         for(int i=0;i<list.size();i++){  
    37.             Element elt = (Element) list.get(i);  
    38.             Node node = elt.selectSingleNode("Definition");  
    39.             if(node == null){  
    40.                 Element info = elt.addElement("Definition");  
    41.                 info.addAttribute("xml:space", "preserve");  
    42.                 info.addText(elt.selectSingleNode("Name").getText());  
    43. //              elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());  
    44.             }else{  
    45.                 elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());  
    46.             }  
    47.               
    48.         }  
    49.           
    50.         /* 
    51.          * 修改AttributeProps 
    52.          */  
    53.         list = doc.selectNodes("//Entity_Groups//Entity//Attribute_Groups//Attribute//AttributeProps");  
    54.         System.out.println("需要修改的AttributeProps一共有:"+list.size()+"处。");  
    55.         for(int i=0;i<list.size();i++){  
    56.             Element elt = (Element) list.get(i);  
    57.             Node node = elt.selectSingleNode("Definition");  
    58.             if(node == null){  
    59.                                 //增加节点  
    60.                 Element info = elt.addElement("Definition");  
    61.                 info.addAttribute("xml:space", "preserve");  
    62.                 info.addText(elt.selectSingleNode("Name").getText());  
    63. //              elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());  
    64.             }else{  
    65.                 elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());  
    66.             }  
    67.         }  
    68.         System.out.println("修改完毕!");  
    69.           
    70.         XMLWriter writer = new XMLWriter(new FileOutputStream(file));  
    71.         writer.write(doc);  
    72.         writer.close();  
    73.           
    74.     }  
    75.       
    76.     private void getProperties(String xmlProperty) throws IOException{  
    77.         //String configFile = xmlProperty;  
    78.         String configFile = "D:/Workspaces for MyEclipse 7.1/RefreshXml/src/xml.properties";  
    79.         InputStream is = new BufferedInputStream(new FileInputStream(new File(configFile)));  
    80.         if(is == null){  
    81.             is = this.getServletContext().getResourceAsStream(configFile);  
    82.         }  
    83.         Properties ps = new Properties();  
    84.         ps.load(is);  
    85.         xmlFilePath = ps.getProperty("xmlPath");  
    86.     }    
    87.       
    88.     /** 
    89.      * @param args 
    90.      */  
    91.     public static void main(String[] args) {  
    92.         if (args.length < 1) {  
    93.             System.out.println("请输入程序所需的配置文件路径作为参数:");  
    94.             System.out.println("1、要修改的xml文件的名称。");  
    95.         } else {  
    96.             String xmlProperty = args[0];  
    97.             System.out.println(xmlProperty);  
    98.             RefreshXml rx = new RefreshXml();  
    99.             try {  
    100.                 rx.modifyXml(xmlProperty);  
    101.             } catch (DocumentException e) {  
    102.                 e.printStackTrace();  
    103.             } catch (IOException e) {  
    104.                 e.printStackTrace();  
    105.             }  
    106.         }  
    107.     }  
    108. }  



    配置文件xml.properties中的内容也比较简单:
    xmlPath=C:\test\2222.xml

    如下是程序jar包:
    RefreshXml2.jar

  • 相关阅读:
    数字货币资金费策略
    如何利用CCXT交易数字货币合约
    三分钟玩转微软AI量化投资开源库QLib
    商品期货月度效应的统计
    数字货币无风险收益率又双叒叕扩大了!
    Omega System Trading and Development Club内部分享策略Easylanguage源码 (第二期)
    【mac】安装配置 homebrew, Nginx
    【Typescript+Vue】01. easy
    【python】sudo python -m SimpleHTTPServer
    【windows】docker to vmware
  • 原文地址:https://www.cnblogs.com/libin6505/p/10132642.html
Copyright © 2020-2023  润新知