用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代码如下:

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中的内容也比较简单:
[color=blue]xmlPath=C\:\\test\\2222.xml[/color]

如下是程序jar包:
[url=http://dl.iteye.com/topics/download/20aa7560-8d20-330f-b9ae-ebf33b1c7438]RefreshXml2.jar[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值