[置顶] 通用XML处理工具

package com.metarnet.Execution.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
 * XML文件通用处理工具
 * @author 程序员 闫帆
 */
public class XMLUtil {
	private Document doc=null;
	private File XMLFile;
	protected static Logger logger=Logger.getLogger(XMLUtil.class);
	public XMLUtil(File XMLFile){
		this.XMLFile=XMLFile;
		SAXReader saxReader = new SAXReader();
		try {
			doc=saxReader.read(XMLFile);
		} catch (DocumentException exception) {
			logger.error("不能读取指定的文件,请确认指定的文件为XML文件!>>>文件路径:"+XMLFile.getAbsolutePath());
			createXML();
		}
	}
	/**
	 * 根据文件路径获取实例(文件路径)
	 * @throws DocumentException 
	 */
	public static XMLUtil getXML(String filepath)
	{
		XMLUtil util=new XMLUtil(new File(filepath));
		return util;
	}
	/**
	 * 根据文件获取实例(File文件)
	 * @throws DocumentException 
	 */
	public static XMLUtil getXML(File file)
	{
		XMLUtil util=new XMLUtil(file);
		return util;
	}
	/**
	 * 获取到第一个匹配项并返回Node
	 */
	public Node SelectNode(String NodeName)
	{
		return doc.selectSingleNode(NodeName);
	}
	/**
	 * 获取到第一个匹配项并返回Element
	 */
	public Element SelectElement(String NodeName)
	{
		return (Element) doc.selectSingleNode(NodeName);
	}
	/**
	 * 获取第一个匹配节点的值
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<Test>1</Test>
	 * </MyInfo>
	 */
	public String SelectNodeValue(String NodeName)
	{
		return SelectNode(NodeName).getText();
	}
	/**
	 * 获取第一个匹配节点的值
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<Node>
	 * 			<Test>1</Test>
	 * 		</Node>
	 * </MyInfo>
	 */
	public String SelectNodeValue(String ElementName,String NodeName)
	{
		return SelectNode(ElementName).valueOf(NodeName);
	}
	/**
	 * 获取第一个匹配节点的值
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<text comment="t1">t2</text>
	 * </MyInfo>
	 */
	public String SelectNodeString(String ElementName,String NodeName)
	{
		return SelectElement(ElementName).attribute(NodeName).getStringValue();
	}
	/**
	 * 添加节点
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<text>t2</text>
	 * </MyInfo>
	 */
	public void addNode(String addNodeName,String addNodeString)
	{
		doc.getRootElement().addElement(addNodeName).addText(addNodeString);
		Save();
	}
	/**
	 * 添加节点
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<text>
	 * 			<text1>t2</text1>
	 * 		</text>
	 * </MyInfo>
	 */
	public void addNode(String ElementName,String addNodeName,String addNodeString)
	{
		SelectElement(ElementName).addElement(addNodeName).addText(addNodeString);
		Save();
	}
	/**
	 * 添加节点
	 * 示例:
	 * <?xml version="1.0" encoding="UTF-8"?>
	 * <MyInfo>
	 * 		<name ID="123"/>
	 * </MyInfo>
	 */
	public void addNodeAttribute(String ElementName,String addNodeName,String addNodeString)
	{
		SelectElement(ElementName).addAttribute(addNodeName, addNodeString);
		Save();
	}
	/**
	 * 删除节点(匹配到第一个节点)
	 */
	public void removeNode(String NodeName)
	{
		SelectNode(NodeName).getParent().remove(SelectNode(NodeName));
		Save();
	}
	/**
	 * 删除节点
	 * (删除父节点下的节点)
	 */
	public void removeNode(String parentNode,String NodeName)
	{
		SelectElement(parentNode).remove(SelectNode(NodeName));
		Save();
	}
	/**
	 * 修改节点值
	 */
	public void alterNode(String NodeName,String NewNodeString)
	{
		SelectNode(NodeName).setText(NewNodeString);
		Save();
	}
	public void createXML()
	{
		doc = DocumentHelper.createDocument();
		doc.addElement("Root");
		Save();
	}
	/**
	 * 保存文件
	 * @throws IOException
	 */
	public void Save()
	{
		OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GB2312");
		XMLWriter write;
		try {
			write = new XMLWriter(new FileWriter(XMLFile),format);
			write.write(doc);
			write.close();
		} catch (IOException e) {
			logger.error("保存文件过程出现错误!",e);
		}
	}
}




以上代码可能新手朋友很难理解,粘贴上测试代码,仅供参考




package com.metarnet.Execution.test;
import org.apache.log4j.Logger;
import com.metarnet.Execution.util.XMLUtil;

public class TestXml {
	public static final Logger logger=Logger.getLogger(TestTelnet.class);
	public static void main(String[] args) {
		XMLUtil util = XMLUtil.getXML("F:\\Test.xml");
		util.createXML();
		util.addNode("name", "闫帆");
		util.addNode("Telephone", "18691090812");
		util.addNodeAttribute("//name", "ID", "123");
		System.out.println(util.SelectNodeValue("//name"));
		System.out.println(util.SelectNodeString("//name", "ID"));
		util.addNode("www", "Metar");
		util.addNode("text", "闫帆");
		util.addNode("//www","Telephone", "18691090812");
		util.alterNode("//text", "修改测试");
		System.out.println(util.SelectNodeValue("//www"));
		util.removeNode("//Telephone");
		util.removeNode("//text","//Telephone");
	}
}





-----------------------------------程序员 闫帆原创---------------------------------------

转载请注明原创人信息  程序员 闫帆yanfanvip



posted @ 2012-08-07 14:16  程序员闫帆  阅读(226)  评论(0编辑  收藏  举报