package _test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
class VolumePlan {
private String beginTime;
private String endTime;
private int volume;
public String getBeginTime() {
return beginTime;
}
public void setBeginTime(String beginTime) {
this.beginTime = beginTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public int getVolume() {
return volume;
}
public void setVolume(int volume) {
this.volume = volume;
}
}
public class TestXML {
public static void writeXML(String filename, Document doc) {
try {
/** 格式化输出,类型IE浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();// 这句指定了格式化的方式为缩进式,则非紧凑式
/** 指定XML编码 */
//format.setEncoding("UTF-8");//默认为UTF-8,可不指定。
// 注意:此处用FileOutputStream而非FileWriter,可解决存入文件非用UTF8编码而用ANSI编码的问题
Writer writer = new OutputStreamWriter(new FileOutputStream(
filename), "UTF-8");
XMLWriter output = new XMLWriter(writer, format);
output.write(doc);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static Document readXML(String filename) {
SAXReader saxReader = new SAXReader();
try {
// 注意:此处用FileInputStream而非FileReader(new
// File(filename)),用UTF8编码保存文件修改后中文乱码的问题
Document doc = saxReader.read(new FileInputStream(filename),
"UTF-8");
return doc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Document createTimeVolumeXML(VolumePlan vol) {
Document doc = DocumentHelper.createDocument();
doc.addComment("这是一个音量计划表");
Element elPlan = doc.addElement("根结点");
Element elTimeSpan = elPlan.addElement("timespan");
Element elBegintime = elTimeSpan.addElement("begintime");
elBegintime.setText(vol.getBeginTime());
Element elEndtime = elTimeSpan.addElement("endtime");
elEndtime.setText(vol.getEndTime());
Element elVolume = elTimeSpan.addElement("volume");
elVolume.setText(Integer.toString(vol.getVolume()));
return doc;
}
public static Document addTimeVolume2XML(Document doc, VolumePlan vol) {
Element rootNode = (Element) doc.selectSingleNode("根结点");
Element elTimeSpan = rootNode.addElement("timespan");
elTimeSpan.addComment("这是一个新加的节点");
Element elBegintime = elTimeSpan.addElement("begintime");
elBegintime.setText(vol.getBeginTime());
Element elEndtime = elTimeSpan.addElement("endtime");
elEndtime.setText(vol.getEndTime());
Element elVolume = elTimeSpan.addElement("volume");
elVolume.setText(Integer.toString(vol.getVolume()));
return doc;
}
public static Document editTimeVolume2XML(Document doc, VolumePlan orgVol,
VolumePlan newVol) {
String begintime = orgVol.getBeginTime();
List nodeList = doc.selectNodes("根结点/timespan/begintime");
Iterator iterator = nodeList.iterator();
while (iterator.hasNext()) {
Element node = (Element) iterator.next();
if (begintime.equals(node.getText())) {
node.getParent().addComment("这个节点被修改过");
node.setText(newVol.getBeginTime());
node.getParent().selectSingleNode("endtime").setText(
newVol.getEndTime());
node.getParent().selectSingleNode("volume").setText(
Integer.toString(newVol.getVolume()));
}
}
return doc;
}
public static Document delTimeVolume2XML(Document doc, VolumePlan vol) {
String begintime = vol.getBeginTime();
List nodeList = doc.selectNodes("根结点/timespan/begintime");
Iterator iterator = nodeList.iterator();
while (iterator.hasNext()) {
Element node = (Element) iterator.next();
if (begintime.equals(node.getText())) {
((Element) doc.selectSingleNode("根结点"))
.remove(node.getParent());
break;
}
}
return doc;
}
public static void main(String[] args) {
VolumePlan vol = new VolumePlan();
vol.setBeginTime("09:00:00");
vol.setEndTime("10:00:00");
vol.setVolume(100);
Document doc = createTimeVolumeXML(vol);
VolumePlan volNew = new VolumePlan();
volNew.setBeginTime("10:00:00");
volNew.setEndTime("11:00:00");
volNew.setVolume(50);
doc = addTimeVolume2XML(doc, volNew);
VolumePlan volUpdate = new VolumePlan();
volUpdate.setBeginTime("10:00:00");
volUpdate.setEndTime("11:00:00");
volUpdate.setVolume(0);
doc = editTimeVolume2XML(doc, volNew, volUpdate);
doc = delTimeVolume2XML(doc, vol);
writeXML("d://volumeplan.xml", doc);
Document newDoc = readXML("d://volumeplan.xml");
newDoc = addTimeVolume2XML(newDoc, vol);
newDoc = editTimeVolume2XML(newDoc, volUpdate, volNew);
newDoc = delTimeVolume2XML(newDoc, volNew);
writeXML("d://volumeplan.xml", newDoc);
}
}
中文问题参考文章1(http://blog.bc-cn.net/user14/70447/archives/2007/6720.shtml)
|
中文问题参考文章2(http://publishblog.blogdriver.com/blog/tb.b?diaryID=501763 ):
读xml文件时:
不要以为你的xml文件写明encoding="utf-8",它就会聪明的帮你用utf-8读哦,必须这样写。
document = reader.read(new FileInputStream(filename), "utf-8");
写xml文件的时候,不要自己创建Writer,让XmlWriter去根据你的form的encoding创建吧。
OutputFormat form = OutputFormat.createPrettyPrint();
form.setEncoding("utf-8");
OutputStream fos = new FileOutputStream(filename);
XMLWriter output = new XMLWriter(fos, form);
可以看到,其实Dom4j对utf-8的支持是很好的,这个也是得益于java新版本对utf-8的支持。在它IO的Reader和Writer类中,都提供了方便指定encoding的方法。
所以,如果以后自己对文件操作,需要涉及到文本的encoding操作,尽量使用Reader和Writer类,而不是File或者自己去操作Stream,除非是需要处理二机制的文件。
- 作者: AndyYehoo 2005年01月16日, 星期日 15:42
格式化输出问题参见(http://www.cnblogs.com/myqiao/archive/2006/12/10/588226.html)
|
/**
*
格式化
XML
文档
,
并解决中文问题
* @param filename
* @return
*/
public int formatXMLFile(String filename){
int returnValue = 0;
try{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
XMLWriter writer = null;
/**
格式化输出
,
类型
IE
浏览一样
*/
OutputFormat format = OutputFormat.createPrettyPrint();
/**
指定
XML
编码
*/
format.setEncoding("GBK");
writer= new XMLWriter(new FileWriter(new File(filename)),format);
writer.write(document);
writer.close();
/**
执行成功
,
需返回
1
*/
returnValue = 1;
}catch(Exception ex){
ex.printStackTrace();
}
return returnValue;
}
|