📄 xmltool.java
字号:
package net.meybo.util;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
/***************************************
* <p>Title:处理 XML 文件程序</p>
* <p>Description:
* 对Dom4J进行简单的简化及封装,负责XML
* </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: 脉博软件</p>
* @author 阚吉彬
* @version 1.0
***************************************/
public final class XMLTool{
static String encoding = "GBK";
// public static void main(String args[]) {
// try {
//
// } catch(Exception ex) {
// ex.printStackTrace();
// }
// }
private XMLTool() {
}
/**
* 设置 XML 的编码 encding
* @param encding:UTF-8/GBK/GB2312
*/
public static void setEncoding(String encding) {
encoding = encding;
}
/**
* 将一个 XML 字符串,转换成一个 Element
* @param xmlStr:一个正确的,良好的 XML 字符串
* @return
* @throws java.lang.Exception
*/
public static Element loadXML(String xmlStr) {
Element el = null;
try {
el=DocumentHelper.createElement(xmlStr);
} catch(Exception e) {
e.printStackTrace();
// throw new Exception("Load xml string error!", e);
}
return el;
}
/**
* 从一个磁盘路径导入 XML
* @param fileName
* @return
* @throws java.lang.Exception
*/
public static synchronized Element load(String fileName) throws Exception {
Element el = null;
try {
SAXReader reader = new SAXReader();
File file=new File(fileName);
Document document = file.exists()?reader.read(fileName):null;
el=document.getRootElement();
} catch(Exception e) {
throw new Exception("Load xml string error!", e);
}
return el;
}
/**
* 保存 XML 到磁盘上
* @param el
* @param fileName
* @throws java.lang.Exception
*/
public static void save(Element el, String fileName) throws Exception {
save(el, fileName, encoding);
}
/**
* 保存 XML 到磁盘上,要传送 XML 编码 encoding
* @param el:Element
* @param fileName:带路径的文件名
* @param encoding;GBK/UTF-8/GB2312
* @throws java.lang.Exception
*/
public static synchronized void save(Element el, String fileName, String encoding) throws Exception {
try {
XMLUtil.toXmlFile(XMLUtil.buildDocument(el), encoding, fileName);
} catch(Exception e) {
throw new Exception("Save xml string error!", e);
}
}
/**
* 将 Element 转换成 XML 字符串
* @param el
* @return
* @throws java.lang.Exception
*/
public static String toXMLString(Element el) throws Exception {
try{
if (el == null)
throw new NullPointerException();
return el.asXML();
} catch(Exception e) {
throw new Exception("to xml string error!");
}
}
public static synchronized String getNodeValue(String fileName,String node) {
String tmpstr="";
try{
Element config=XMLTool.load(fileName);
if(config!=null)
tmpstr=config.selectSingleNode(node).getText();
}
catch(Exception e)
{
}
return tmpstr;
}
/**
* 将 Element 转换成 XML 字符串,要传送 XML 编码 encoding
* @param el
* @param encoding:GBK/UTF-8/GB2312
* @return
* @throws java.lang.Exception
*/
public static String toXMLString(Element el, String encoding) throws Exception {
try{
if(el == null)
throw new NullPointerException();
return el.asXML();
} catch(Exception e) {
throw new Exception("to xml string error!");
}
}
/**
* 设置节点的一个属性
* @param element
* @param name
* @param value
*/
public static void setAttribute(Element element, String name, String value) {
if(value != null && element.attribute(name)!=null)
{
element.attribute(name).setText(value);
}
}
/**
* 设置节点的多个属性
* @param element
* @param attrList
*/
public static void setAttributes(Element element, List attrList) {
int len = attrList.size();
for(int i = 0; i < len; i++) {
Attribute attr = (Attribute)attrList.get(i);
setAttribute(element, attr.getName(), attr.getValue());
}
}
/**
* 设置节点的一个属性
* @param element
* @param name
* @param value
*/
public static void addAttribute(Element element, String name, String value) {
if(value != null)
if(element.attribute(name) == null)
element.add(element.addAttribute(name,value));
else
throw new RuntimeException("Attribute already exist! attribute name is " + name);
}
/**
* 获得节点属性值
* @param element
* @param name
* @return
*/
public static String getAttributeValue(Element element, String name) {
String attributeValue = element.attributeValue(name);
if(attributeValue == null)
attributeValue = "";
return attributeValue;
}
/**
* 获得节点属性名
* @param element
* @param name
* @return
*/
public static String getAttributeString(Element element, String name) {
String attributeValue =element.attributeValue(name);
if(attributeValue == null)
attributeValue = "";
return attributeValue;
}
public static int getIndex(List elementLT, String name, String value)
{
int index = -1;
if(value != null)
{
int listLength = elementLT.size();
for(int i = 0; i < listLength; i++)
{
String attrValue = ((Element)elementLT.get(i)).attributeValue(name);
if(attrValue == null || !attrValue.equals(value))
continue;
index = i;
break;
}
}
return index;
}
public static List getAllChildren(Element theEL)
{
List childrens = new ArrayList(100);
List lt = theEL.elements();
childrens.addAll(lt);
for(int i = 0; i < lt.size(); i++)
childrens.addAll(getAllChildren((Element)lt.get(i)));
return childrens;
}
public static int getDeepness(Element el)
{
int deepness[] = {
0, 0
};
getDeepness(el, deepness);
return deepness[1];
}
private static void getDeepness(Element el, int deepness[])
{
deepness[0]++;
if(deepness[0] > deepness[1])
deepness[1] = deepness[0];
List elList = el.elements();
int ellength = elList.size();
for(int i = 0; i < ellength; i++)
{
Element subEl = (Element)elList.get(i);
getDeepness(subEl, deepness);
deepness[0]--;
}
}
public static Element copyAttributesToElement(Element srcEL, Element tgtEL, int copyType)
{
List attrs = srcEL.attributes();
int len = attrs.size();
for(int i = 0; i < len; i++)
{
Attribute attr = (Attribute)attrs.get(i);
Attribute attr2 = tgtEL.attribute(attr.getName());
if(attr2 == null)
tgtEL.add((Attribute)attr.clone());
else
if(copyType == 0)
attr2.setValue(attr.getValue());
}
return tgtEL;
}
public static Element copyAttributesToElement(List srcAttrs, Element tgtEL, int copyType)
{
int len = srcAttrs.size();
for(int i = 0; i < len; i++)
{
Attribute attr = (Attribute)srcAttrs.get(i);
Attribute attr2 = tgtEL.attribute(attr.getName());
if(attr2 == null)
tgtEL.add((Attribute)attr.clone());
else
if(copyType == 0)
attr2.setValue(attr.getValue());
}
return tgtEL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -