使用dom4j修改解析xml,xml文件的位置是配置在xml.properties文件中,程序打成jar包,在命令行中执行如下命令即可:
[img]http://dl.iteye.com/upload/attachment/150593/6b4ac972-cf45-3582-9a04-5e88bd0c962d.jpg" alt="[/img]
主要的java代码如下:
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Properties;
- import javax.servlet.http.HttpServlet;
- import org.dom4j.Document;
- import org.dom4j.DocumentException;
- import org.dom4j.Element;
- import org.dom4j.Node;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- public class RefreshXml extends HttpServlet {
- private String xmlFilePath="";
- public void modifyXml(String xmlProperty) throws DocumentException, IOException {
- this.getProperties(xmlProperty);
- File file = new File(xmlFilePath);
- SAXReader reader = new SAXReader();
- Document doc = reader.read(file);
- List list;
- /*
- * 修改EntityProps
- */
- list = doc.selectNodes("//Entity_Groups//Entity//EntityProps");
- System.out.println("需要修改的EntityProps一共有:"+list.size()+"处。");
- for(int i=0;i<list.size();i++){
- Element elt = (Element) list.get(i);
- Node node = elt.selectSingleNode("Definition");
- if(node == null){
- Element info = elt.addElement("Definition");
- info.addAttribute("xml:space", "preserve");
- info.addText(elt.selectSingleNode("Name").getText());
- // elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
- }else{
- elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
- }
- }
- /*
- * 修改AttributeProps
- */
- list = doc.selectNodes("//Entity_Groups//Entity//Attribute_Groups//Attribute//AttributeProps");
- System.out.println("需要修改的AttributeProps一共有:"+list.size()+"处。");
- for(int i=0;i<list.size();i++){
- Element elt = (Element) list.get(i);
- Node node = elt.selectSingleNode("Definition");
- if(node == null){
- //增加节点
- Element info = elt.addElement("Definition");
- info.addAttribute("xml:space", "preserve");
- info.addText(elt.selectSingleNode("Name").getText());
- // elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
- }else{
- elt.selectSingleNode("Definition").setText(elt.selectSingleNode("Name").getText());
- }
- }
- System.out.println("修改完毕!");
- XMLWriter writer = new XMLWriter(new FileOutputStream(file));
- writer.write(doc);
- writer.close();
- }
- private void getProperties(String xmlProperty) throws IOException{
- //String configFile = xmlProperty;
- String configFile = "D:/Workspaces for MyEclipse 7.1/RefreshXml/src/xml.properties";
- InputStream is = new BufferedInputStream(new FileInputStream(new File(configFile)));
- if(is == null){
- is = this.getServletContext().getResourceAsStream(configFile);
- }
- Properties ps = new Properties();
- ps.load(is);
- xmlFilePath = ps.getProperty("xmlPath");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- if (args.length < 1) {
- System.out.println("请输入程序所需的配置文件路径作为参数:");
- System.out.println("1、要修改的xml文件的名称。");
- } else {
- String xmlProperty = args[0];
- System.out.println(xmlProperty);
- RefreshXml rx = new RefreshXml();
- try {
- rx.modifyXml(xmlProperty);
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
配置文件xml.properties中的内容也比较简单:
xmlPath=C:\test\2222.xml
如下是程序jar包:
RefreshXml2.jar