你的分享就是我们的动力 ---﹥

处理xml的工具

时间:2013-07-16 14:59来源:www.chengxuyuans.com 点击:

代码简介

项目中难免要处理xml。无论是使用dom4j还是saxReader。都需要写比较多的代码。自己封装了一个。从开始写到现在还没有更新过功能,由此可见已经满足了绝大数的功能。和大家分享一下。

结合xpth功能还是挺强大的。

代码片段

package com.eyumob.common.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
 * 用于解析xml文件
 * @dateTime 2012-5-23 下午04:43:00
 * @description 
 * 注意事项:
 */
public class XmlUtil {
	/**
	 * @param fileName 文件路径
	 * @return
	 */
	public static Document getDocument(File file){
		SAXReader reader = new SAXReader();
        Document document = null;
		try {
			document = reader.read(file);
		} catch (DocumentException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(), e);
		}
        return document;

	}
	/**
	 * 通过文件名获取Document
	 * @param root
	 * @param fileName
	 * @return
	 */
	public static Document getDocumentByFileName(String root,String fileName){
		File file = new File(root,fileName);
		return getDocument(file);
	}
	/**
	 * 获取根节点
	 * @param document
	 * @return
	 */
	public static Element getRoot(Document document){
		return document.getRootElement();
	}
	
	/**
	 * 获取单一节点
	 * @param xpath
	 * @return
	 */
	public static Node getSingleNode(Document document ,String xpath){
		return document.selectSingleNode(xpath);
	}
	/**
	 * 获取节点
	 * @param document
	 * @param xpath
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Node> getNodes(Document document ,String xpath){
		return document.selectNodes(xpath);
	}
	/**
	 * 获取节点的属性值
	 * @param node
	 * @param properties
	 * @return
	 */
	public static String getValue(Node node,String properties){
		String properites = "@" + properties;
		return node.valueOf(properites);
	}
	/**
	 * 更改节点的属性值
	 * @param node
	 * @param name
	 * @param value
	 */
	public static void setAttribute(Node node,String name,String value){
		Element element = (Element) node;
		element.addAttribute(name, value);
	}
	/**
	 * 设置text的值
	 * @param node
	 * @param value
	 */
	public static void setText(Node node,String value){
		node.setText(value);
	}
	/**
	 * 保存文档
	 * @param document
	 */
	public static void saveXml(Document document,String fileName){
		XMLWriter writer;
		try {
			writer = new XMLWriter(new FileWriter(fileName));
			writer.write( document );
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage(),e);
		}
	}
}

代码片段

相应的测试方法如下:
File file = new File("uploadFile.xml");
		Document document = XmlUtil.getDocument(file);
		String openSizeLimit = XmlUtil.getSingleNode(document, "//uploadConfigeration/openSizeLimit").getText();
		String maxFileSize = XmlUtil.getSingleNode(document, "//uploadConfigeration/maxFileSize").getText();
		String totalFileSize = XmlUtil.getSingleNode(document, "//uploadConfigeration/totalFileSize").getText();
		String uploadPath = XmlUtil.getSingleNode(document, "//uploadConfigeration/uploadPath").getText();
		String openFileScattered = XmlUtil.getSingleNode(document, "//uploadConfigeration/openFileScattered").getText();
		String openFileTypeLimit = XmlUtil.getSingleNode(document, "//uploadConfigeration/openFileTypeLimit").getText();
		List<Node> nodes = XmlUtil.getNodes(document, "//uploadConfigeration/fileTypes/fileType");
		String openProgressListener = XmlUtil.getSingleNode(document, "//uploadConfigeration/openProgressListener").getText();
		List<String> extensionList = new ArrayList<String>();
		for (Node node : nodes) {
			extensionList.add(node.getText());
		}

转载注明地址http://www.chengxuyuans.com/code/java/65828.html