📄 xmlparseutil.java
字号:
package com.singnet.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.singnet.data.Constants;
public class XMLParseUtil {
private static XMLParseUtil _instance = null;
private Document doc;
private String path;
public synchronized static XMLParseUtil getInstance() {
if(null == _instance) {
_instance = new XMLParseUtil();
}
return _instance;
}
private XMLParseUtil() {
System.out.println("I'm Created!");
this.path = Constants.ROOT_PATH + Constants.CFG_FILE;
this.doc = getXMLDoc();
System.out.println("Path : " + this.path);
}
public String getNodeValue(String nodeName) {
Node node = getNode(nodeName);
return node.getText();
}
public void setNodeValue(String nodeName, String value) {
Node node = getNode(nodeName);
node.setText(value);
}
public void saveNode() {
try {
OutputFormat format = new OutputFormat(" ", true, "GBK");
XMLWriter writer = new XMLWriter(new FileWriter(this.path), format);
writer.write(doc);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Document getXMLDoc() {
SAXReader reader = new SAXReader();
Document doc;
try {
doc = reader.read(new FileInputStream(this.path));
return doc;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private Element getRootElement(Document doc) {
return doc == null ? null : doc.getRootElement();
}
private Node getNode(String nodeName) {
return doc.selectSingleNode("//" + nodeName);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -