xml文件如下 配置功能开关
<Configurations>
<Samples>true</Samples>
<Excepts>true</Excepts>
<CheckFace>false</CheckFace>
<ThumbNail>false</ThumbNail>
<ScanDir>xuzhen</ScanDir>
<SampleDir>G</SampleDir>
<ExceptDir>G:</ExceptDir>
<PicScanDB>G:</PicScanDB>
<DBName>xuzhen</DBName>
<DBHost>sdfsdfsdf</DBHost>
<DBUser>xuzhen</DBUser>
<EthIP>192.168.0.117</EthIP>
<DBPassWd>xuzhen</DBPassWd>
<Spec>*.jpg;*.jpeg;*.jpe;*.jfif;*.bmp;*.dib</Spec>
<After>1980-01-02 00:00:00</After>
<InitThreadNum>20</InitThreadNum>
<MaxThreadNum>40</MaxThreadNum>
<LogLevel>9999</LogLevel>
<DeleteChecked>FALSE</DeleteChecked>
</Configurations>
操作类
package com.ibstech.controller.syntax;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.InvalidPropertiesFormatException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Entity;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
public class ServerManager {
public static HashMap GetXML() {
HashMap hm = new HashMap();
try {
String xmlFile = "C://Documents and Settings//Administrator//workspace//MyTest//WebContent//config.xml";
File file = new File(xmlFile);
if (file.exists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodeList = doc.getChildNodes();
Node nod = nodeList.item(0);
nodeList = nod.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
nod = nodeList.item(i);
// 是文本
if (nod.getNodeType() != 3) {
hm.put(nod.getNodeName(), nod.getTextContent());
System.out.println("键" + nod.getNodeName() + "值"
+ nod.getTextContent());
}
}
}
} catch (Exception ex) {
}
return hm;
}
public static void SetXML(HashMap map) {
//map用来存放最新设置的参数在下面将map得到的与以前相比较
//如果xml中节点的名字与这个相同则就更新他的context内容
try {
String xmlFile = "C://Documents and Settings//Administrator//workspace//MyTest//WebContent//config.xml";
File file = new File(xmlFile);
if (file.exists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(file);
NodeList nodeList = doc.getChildNodes();
Node nod = nodeList.item(0);
nodeList = nod.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
nod = nodeList.item(i);
if (nod.getNodeType() != 3) {
//遍历我得到节点与xml中通过tagname比较如果有的就把他的context改为我现在设置的
//迭代器用来遍历map
Iterator it=map.entrySet().iterator();
while(it.hasNext())
{
Map.Entry ent=(Map.Entry)it.next();
if (nod.getNodeName() ==ent.getKey()) {
nod.setTextContent((String) ent.getValue());
//向xml中写入
OutputFormat format = new OutputFormat(doc);
format.setOmitXMLDeclaration(true);
XMLSerializer output = new XMLSerializer(new java.io.FileWriter(xmlFile, false), format);
output.serialize(doc);
}
}
}
}
}
} catch (Exception ex) {
}
}
public static void main(String[] args) {
//ServerManager.SetXML();
System.out.println(ServerManager.GetXML());
File file=new File("");
System.out.println(file.getAbsolutePath());
}
}