此小程序用DOM来读取XML文件,用DOM4j来写XML文件。
DOM4j下载地址:http://sourceforge.net/projects/dom4j
1、Applet主程序mainFrame.java
package frame;
/**
* @author wls
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class mainFrame
extends Applet {
DomXml domxml = new DomXml();
Button btnExit = new Button("Exit");
Button btnRun = new Button("Save");
Label lblMain = new Label("dynamicroute.xml配置文件");
Label lblSiteinfo = new Label("srcserverid:");
Label lblPath = new Label("destserverid:");
Label lblFilename = new Label("switchserver:");
Label lblSession = new Label("metric:");
Label lblSuccess = new Label(" ");
TextField txtSiteinfo = new TextField(domxml.getSrcserverid(), 10);
TextField txtPath = new TextField(domxml.getDestserverid(), 10);
TextField txtFilename = new TextField(domxml.getSwitchserver(), 10);
TextField txtSession = new TextField(domxml.getMetric(), 10);
Panel p6 = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel p4 = new Panel();
Panel p5 = new Panel();
Panel p7 = new Panel();
Panel p8 = new Panel();
public void init() {
resize(400, 300);
p6.add(lblMain);
p1.add(lblSiteinfo);
p1.add(txtSiteinfo);
p2.add(lblPath);
p2.add(txtPath);
p3.add(lblSession);
p3.add(txtSession);
p4.add(btnRun);
p4.add(btnExit);
p5.add(lblFilename);
p5.add(txtFilename);
p8.add(lblSuccess);
setLayout(new GridLayout(4, 2, 1, 1));
add(p6);
add(p7);
add(p1);
add(p2);
add(p5);
add(p3);
add(p4);
add(p8);
}
public boolean action(Event evt, Object obj) {
if (evt.target instanceof Button) {
String btnName = (String) obj;
if (btnName.equals("Save")) {
try {
domxml.setSrcserverid(txtSiteinfo.getText());
domxml.setDestserverid(txtPath.getText());
domxml.setSwitchserver(txtFilename.getText());
domxml.setMetric(txtSession.getText());
buildXml buildxml = new buildXml();
if (buildxml.createXMLFile("dynamicroute.xml", domxml) == 1 &&
buildxml.formatXMLFile("dynamicroute.xml")==1) {
lblSuccess.setText("Save success!");
System.out.println("Save success!");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
else if (btnName.equals("Exit")) {
System.exit(0);
}
}
return super.action(evt, obj);
}
public static void main(String args[]) {
new FrameDownload(new mainFrame());
}
}
class FrameDownload
extends Frame {
public FrameDownload(Applet applet) {
setSize(400, 300);
add("Center", applet);
applet.init();
setVisible(true);
this.addWindowListener(new closeWindows());
}
}
class closeWindows
extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Frame frm = (Frame) (e.getSource());
frm.dispose();
System.exit(0);
}
}
2、读取XML类DomXml.java
package frame;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/*
* Created on 2004-8-19
* @author wls
*java读取XML文档
*利用DoM来读取一个XML文档的内容
*
*/
public class DomXml {
private String srcserverid;
private String destserverid;
private String switchserver;
private String metric;
public DomXml() {
try {
initXml("dynamicroute.xml");
}
catch (Exception ex) {
metric="0";
}
}
public String getSrcserverid() {
return srcserverid;
}
public void setSrcserverid(String srcserverid) {
this.srcserverid = srcserverid;
}
public String getDestserverid() {
return destserverid;
}
public void setDestserverid(String destserverid) {
this.destserverid = destserverid;
}
public String getSwitchserver() {
return switchserver;
}
public void setSwitchserver(String switchserver) {
this.switchserver = switchserver;
}
public String getMetric() {
return metric;
}
public void setMetric(String metric) {
this.metric = metric;
}
public void initXml(String fileName) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder bulider = factory.newDocumentBuilder();
Document doc = bulider.parse(fileName);
NodeList nl = doc.getElementsByTagName("dynamicroute");
for (int i = 0; i < nl.getLength(); i++) {
Element node = (Element) nl.item(i);
srcserverid = node.getElementsByTagName("srcserverid").item(0).
getFirstChild().getNodeValue().trim();
destserverid = node.getElementsByTagName("destserverid").item(0).
getFirstChild().getNodeValue().trim();
switchserver = node.getElementsByTagName("serverid").item(0).
getFirstChild().getNodeValue().trim();
metric = node.getElementsByTagName("metric").item(0).getFirstChild().
getNodeValue().trim();
}
}
}
3、写XML文件类buildXml.java
package frame;
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.*;
/**
* @author wls
*/
public class buildXml {
public buildXml() {
}
/**
* 建立一个XML文档,文档名由输入参数决定
* @param filename 需建立的文件名
* @return 返回操作结果, 0表失败, 1表成功
*/
public int createXMLFile(String filename, DomXml domxml) {
/** 返回操作结果, 0表失败, 1表成功 */
int returnValue = 0;
/** 建立document对象 */
Document document = DocumentHelper.createDocument();
/** 建立XML文档的根route */
Element routeElement = document.addElement("route");
/** 加入第一个dynamicroute节点 */
Element dynamicrouteElement = routeElement.addElement("dynamicroute");
/** 加入show参数内容 */
//bookElement.addAttribute("show","yes");
/** 加入节点 */
Element srcserveridElement = dynamicrouteElement.addElement("srcserverid");
Element destserveridElement = dynamicrouteElement.addElement("destserverid");
Element switchserverElement = dynamicrouteElement.addElement("switchserver");
Element serveridElement = switchserverElement.addElement("serverid");
Element metricElement = dynamicrouteElement.addElement("metric");
/** 为title设置内容 */
srcserveridElement.setText(domxml.getSrcserverid());
destserveridElement.setText(domxml.getDestserverid());
serveridElement.setText(domxml.getSwitchserver());
metricElement.setText(domxml.getMetric());
try {
/** 将document中的内容写入文件中 */
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
writer.write(document);
writer.close();
/** 执行成功,需返回1 */
returnValue = 1;
}
catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
/**
* 格式化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 output = null;
/** 格式化输出,类型IE浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码 */
format.setEncoding("UTF-8");
output = new XMLWriter(new FileWriter(new File(filename)), format);
output.write(document);
output.close();
/** 执行成功,需返回1 */
returnValue = 1;
}
catch (Exception ex) {
ex.printStackTrace();
}
return returnValue;
}
}