📄 mazingdom4j.java
字号:
package com.sinosoft.security.util;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
public class MazingDom4j {
public MazingDom4j() {
super();
// TODO 自动生成构造函数存根
}
// 通过xml文件名得到DOM
public static Document getDocument(String xmlFileName) throws DocumentException {
SAXReader reader = new SAXReader();
Document d = reader.read(new File(xmlFileName));
return d;
}
// 重载,通过xml文件内容得到DOM
public static Document getDocument(String xmlContent, boolean b)
throws DocumentException {
Document d = DocumentHelper.parseText(xmlContent);
return d;
}
// 输出字符串
public static String transformDOM(Document d) {
String xmlContent = "";
xmlContent = d.asXML();
return xmlContent;
}
// 得到节点
public static Element getNode(Document d, String elePath, String eleValue) {
Element ele = null;
List l = d.selectNodes(elePath);
Iterator iter = l.iterator();
while (iter.hasNext()) {
Element tmp = (Element) iter.next();
if (tmp.getText().equals(eleValue)) {
ele = tmp;
}
}
return ele;
}
// 重载,得到节点
public static Element getNode(Document d, String elePath) {
Element ele = (Element) d.selectSingleNode(elePath);
return ele;
}
// 增加节点
public static void addNode(Element parentEle, String eleName, String eleValue) {
Element newEle = parentEle.addElement(eleName);
newEle.setText(eleValue);
}
// 增加属性节点
public static void addAttribute(Element ele, String attributeName,
String attributeValue) {
ele.addAttribute(attributeName, attributeValue);
}
// 删除节点
public static void removeNode(Element parentEle, String eleName, String eleValue) {
Iterator iter = parentEle.elementIterator();
Element delEle = null;
while (iter.hasNext()) {
Element tmp = (Element) iter.next();
if (tmp.getName().equals(eleName) && tmp.getText().equals(eleValue)) {
delEle = tmp;
}
}
if (delEle != null) {
parentEle.remove(delEle);
}
}
// 删除属性
public static void removeAttr(Element ele, String attributeName) {
Attribute att = ele.attribute(attributeName);
ele.remove(att);
}
// 修改节点值
public static void setNodeText(Element ele, String newValue) {
ele.setText(newValue);
}
// 修改属性值
public static void setAttribute(Element ele, String attributeName,
String attributeValue) {
Attribute att = ele.attribute(attributeName);
att.setText(attributeValue);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -