⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bdom4j.java

📁 dom4j与Transform配合操作XML文档,并调用Xslt进行格式化的例子
💻 JAVA
字号:
package com.zhujiayun.dom4j;

import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.SAXReader;


public class BDom4j {
	/** XML文件路径 */
	private String XMLPath = null;

	/** XML文档 */
	private Document document = null;

	public BDom4j() {
	}

	/**
	 * 初始化xml文件
	 * 
	 * @param XMLPath
	 *            文件路径
	 */
	public BDom4j(String XMLPath) {
		this.XMLPath = XMLPath;
	}

	/**
	 * 打开文档
	 */
	public void openXML() {
		try {
			SAXReader reader = new SAXReader();
			this.document = reader.read(this.XMLPath);
			System.out.println("openXML() successful ...");
		} catch (Exception e) {
			System.out.println("openXML() Exception:" + e.getMessage());
		}
	}

	/**
	 * 创建文档
	 * 
	 * @param rootName
	 *            根节点名称
	 */
	public void createXML(String rootName) {
		try {
			this.document = DocumentHelper.createDocument();
			Element root = document.addElement(rootName);
			System.out.println("createXML() successful...");
		} catch (Exception e) {
			System.out.println("createXML() Exception:" + e.getMessage());
		}
	}

	/**
	 * 添加根节点的child
	 * 
	 * @param nodeName
	 *            节点名
	 * @param nodeValue
	 *            节点值
	 */
	public void addNodeFromRoot(String nodeName, String nodeValue) {
		Element root = this.document.getRootElement();
		Element level1 = root.addElement(nodeName);
		level1.addText(nodeValue);
	}

	/**
	 * 打开文档
	 * 
	 * @param filePath
	 *            文档路径
	 */
	public void openXML(String filePath) {
		try {
			SAXReader saxReader = new SAXReader();
			this.document = saxReader.read(filePath);
			System.out.println("openXML(String filePath) successful ...");
		} catch (Exception e) {
			System.out.println("openXML() Exception:" + e.getMessage());
		}
	}

	/**
	 * 保存文档
	 */
	public void saveXML() {
		try {
			XMLWriter output = new XMLWriter(new FileWriter(new File(
					this.XMLPath)));
			output.write(document);
			output.close();
			System.out.println("saveXML() successful ...");
		} catch (Exception e1) {
			System.out.println("saveXML() Exception:" + e1.getMessage());
		}
	}

	/**
	 * 保存文档
	 * 
	 * @param toFilePath
	 *            保存路径
	 */
	public void saveXML(String toFilePath) {
		try {
			XMLWriter output = new XMLWriter(new FileWriter(
					new File(toFilePath)));
			output.write(document);
			output.close();
		} catch (Exception e1) {
			System.out.println("saveXML() Exception:" + e1.getMessage());
		}
	}

	/**
	 * 获得某个节点的值
	 * 
	 * @param nodeName
	 *            节点名称
	 */
	public String getElementValue(String nodeName) {
		try {
			Node node = document.selectSingleNode("//" + nodeName);
			return node.getText();
		} catch (Exception e1) {
			System.out
					.println("getElementValue() Exception:" + e1.getMessage());
			return null;
		}
	}

	/**
	 * 获得某个节点的子节点的值
	 * 
	 * @param nodeName
	 * @param childNodeName
	 * @return
	 */
	public String getElementValue(String nodeName, String childNodeName) {
		try {
			Node node = this.document.selectSingleNode("//" + nodeName + "/"
					+ childNodeName);
			return node.getText();
		} catch (Exception e1) {
			System.out
					.println("getElementValue() Exception:" + e1.getMessage());
			return null;
		}
	}
	
	/**
	 * 设置一个节点的text
	 * 
	 * @param nodeName
	 *            节点名
	 * @param nodeValue
	 *            节点值
	 */
	public void setElementValue(String nodeName, String nodeValue) {
		try {
			Node node = this.document.selectSingleNode("//" + nodeName);
			node.setText(nodeValue);
		} catch (Exception e1) {
			System.out
					.println("setElementValue() Exception:" + e1.getMessage());
		}
	}

	/**
	 * 设置一个节点值
	 * 
	 * @param nodeName
	 *            父节点名
	 * @param childNodeName
	 *            节点名
	 * @param nodeValue
	 *            节点值
	 */
	public void setElementValue(String nodeName, String childNodeName,
			String nodeValue) {
		try {
			Node node = this.document.selectSingleNode("//" + nodeName + "/"
					+ childNodeName);
			node.setText(nodeValue);
		} catch (Exception e1) {
			System.out
					.println("setElementValue() Exception:" + e1.getMessage());
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -