📄 domconfig.java
字号:
package lib.commons.config;
import org.dom4j.Node;
import org.dom4j.Document;
//import org.dom4j.DocumentHelper;
import org.dom4j.io.SAXReader;
import java.util.List;
import java.util.Iterator;
public class DomConfig {
private Node node;
public DomConfig(Node node) {
this.node = node;
}
public Node getNode() {
return node;
}
public String getXmlString() {
if (null == node) {
return "";
}
return node.asXML();
}
public boolean isEmpty() {
return null == node;
}
public String getValue() {
return null == node ? null : node.getStringValue();
}
public String selectValue(String xPath) {
if (null == node) {
return null;
}
Node selectNode = node.selectSingleNode(xPath);
if (null == selectNode) {
return null;
}
return selectNode.getStringValue();
}
public DomConfig selectChildConfig(String xPath) {
if (null == node) {
return null;
}
Node selectNode = node.selectSingleNode(xPath);
return new DomConfig(selectNode);
}
public String[] selectValues(String xPath) {
if (null == node) {
return null;
}
List selectNodeList = node.selectNodes(xPath);
if (null == selectNodeList || selectNodeList.size() == 0) {
return null;
}
int size = selectNodeList.size();
String[] nodeValues = new String[size];
Iterator it = selectNodeList.iterator();
for (int i = 0; i < size; i++) {
Node selectNode = (Node) it.next();
nodeValues[i] = selectNode.getStringValue();
}
return nodeValues;
}
public DomConfig[] selectChildConfigs(String xPath) {
if (null == node) {
return null;
}
List selectNodeList = node.selectNodes(xPath);
if (null == selectNodeList || selectNodeList.size() == 0) {
return null;
}
int size = selectNodeList.size();
DomConfig[] childConfigs = new DomConfig[size];
Iterator it = selectNodeList.iterator();
for (int i = 0; i < size; i++) {
Node selectNode = (Node) it.next();
childConfigs[i] = new DomConfig(selectNode);
}
return childConfigs;
}
public static DomConfig loadXmlFile(String filePath) throws Exception {
SAXReader reader = new SAXReader();
//reader.setValidation(false);
//reader.setIncludeInternalDTDDeclarations(false);
//reader.setIncludeExternalDTDDeclarations(false);
//reader.setEntityResolver(null);
Document doc = reader.read(filePath);
return new DomConfig(doc);
}
public static DomConfig loadXml(String xml) throws Exception {
SAXReader reader = new SAXReader();
//reader.setValidation(false);
//reader.setIncludeInternalDTDDeclarations(false);
//reader.setIncludeExternalDTDDeclarations(false);
//reader.setEntityResolver(null);
Document doc = reader.read(new java.io.StringReader(xml));
//Document doc = DocumentHelper.parseText(xml);
return new DomConfig(doc);
}
public static void main(String[] args) throws Exception {
DomConfig d = DomConfig.loadXmlFile("c:/feequery.xml");
System.out.println(d.getXmlString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -