一个使用JDOM读取XML文件的工具类

/*
 * history
 *
 * Created on 2003-5-26
 *
 * 2003-06-05
 *   1.增加了Log的处理信息。
 *   2.抛出的异常由原来UtilException的改变成XMLException,后者继承前者。
 *
 * 2003-09-02 by David Yu
 *   1.增加了改变一个指定元素文本的方法。
 *
 */
package accp.util.xml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;

/**
 * 一个使用JDOM读取XML文件的工具类。
 * 这个类的内部封装了JDOM的具体实现,提供了常用的一些方法,避免直接使用JDOM的API。
 * 同时,也提供了方法可以返回JDOM的一些基本类型。目前的版本中还没有实现关于Namespace
 * 的操作。<br/>
 * <pre>
 * <b>使用了XPath,所以必须使用JDOM-beta9及以上的版本。</b><br/>
 * XML需要引入的包:jdom.jar,xerces.jar,xml-apis.jar,xalan.jar,
 * jaxen-core.jar,jaxen-jdom.jar,saxpath.jar<br/>
 * Log需要引入的包:commons-logging.jar
 * </pre>
 * <pre>
 * 基本示例:<br/>
 *      String filePath = "c:/xx/xxx.xml";
 *      XMLUtil util = XMLUtil.getInsance(filePath);
 *      Element element = util.getSingleElement("/root/elemA/elemB");
 *      String text = util.getSingleElementText("/root/elemA/elemB");
 * </pre>
 *
 * @author Backham Yu
 */
public class XMLUtil
{

    protected static Log log = LogFactory.getLog(XMLUtil.class);

    private Document doc = null;
    //用于快速查询的cache
    private Map lookupCache = new HashMap();

    /**
     * 仅供测试使用
     * @param doc
     */
    XMLUtil(Document doc)
    {
        this.doc = doc;
        if (log.isDebugEnabled())
        {
            log.debug("使用了仅供测试的Constructor。");
        }
    }

    /**
     * 私有的Constructor
     * @param is 流文件
     * @param validate 是否需要验证
     * @throws XMLException
     */
    private XMLUtil(InputStream is, boolean validate) throws XMLException
    {
        SAXBuilder builder = null;
        if (validate == true)
        {
            if (log.isInfoEnabled())
            {
                log.info("需要使用文档验证,可以使用Schema或者DTD。");
            }
            //为了支持Schema,必须进行下面的处理
            builder =
                new SAXBuilder("org.apache.xerces.parsers.SAXParser", true);
            builder.setFeature(
                "http://apache.org/xml/features/validation/schema",
                true);
            builder.setProperty(
                "http://apache.org/xml/properties/schema/external-schemaLocation",
                "http://www.w3.org/2001/XMLSchema-instance");
        }
        else
        {
            builder = new SAXBuilder();
        }

        try
        {
            doc = builder.build(is);
        }
        catch (Exception e)
        {
            if (log.isErrorEnabled())
            {
                log.error("解析XML文件发生异常!" + e.getMessage());
            }
            throw new XMLException(e.getMessage());
        }
    }

    /**
     * 得到一个工具类的实例。默认不需要验证。
     * @param filePath 需要读取的文件路径
     * @return XMLUtil
     * @throws XMLException
     */
    public static XMLUtil getInsance(String filePath) throws XMLException
    {
        return getInsance(filePath, false);
    }

    /**
     * 得到一个工具类的实例。
     * <br/><b>如果指明需要Schema验证,XML文件中指明Schema的路径分隔符号
     * 必须使用左斜线</b>
     * @param filePath 需要读取的文件路径
     * @param validate 是否需要验证
     * @return XMLUtil
     * @throws XMLException
     */
    public static XMLUtil getInsance(String filePath, boolean validate)
        throws XMLException
    {
        try
        {
            return getInsance(new FileInputStream(filePath), validate);
        }
        catch (FileNotFoundException e)
        {
            if (log.isErrorEnabled())
            {
                log.error("读取指定的文件发生异常!" + e.getMessage());
            }
            throw new XMLException(e.getMessage());
        }
    }

    /**
     * 得到一个工具类的实例。默认不需要验证。
     * @param is 流文件
     * @return XMLUtil
     * @throws XMLException
     */
    public static XMLUtil getInsance(InputStream is) throws XMLException
    {
        return getInsance(is, false);
    }

    /**
     * 得到一个工具类的实例。
     * <br/><b>如果指明需要验证,只能使用DTD</b>
     * @param is 流文件
     * @param validate
     * @return XMLUtil
     * @throws XMLException
     */
    public static XMLUtil getInsance(InputStream is, boolean validate)
        throws XMLException
    {
        return new XMLUtil(is, validate);
    }

    /**
     * 把XML文档的内容输出到一个给定的流对象中。默认编码GB2312
     * @param stream 给定的输出流对象
     * @throws XMLException
     */
    public void writeToStream(OutputStream stream) throws XMLException
    {
        writeToStream(stream, "gb2312");
    }

    /**
     * 把XML文档的内容输出到一个给定的流对象中。
     * @param stream 给定的输出流对象
     * @param encoding 指定字符编码。
     * @throws XMLException
     */
    public void writeToStream(OutputStream stream, String encoding)
        throws XMLException
    {
        //表现形式的常量
        final String INDENT = "  ";
        final boolean NEW_LINES = true;

        try
        {
            XMLOutputter out = new XMLOutputter(INDENT, NEW_LINES, encoding);
            out.output(doc, stream);
        }
        catch (Exception e)
        {
            if (log.isErrorEnabled())
            {
                log.error("输出文件到指定的流对象发生异常!" + e.getMessage());
            }
            throw new XMLException(e.getMessage());
        }
    }

    /**
     * 返回所有满足XPath条件的节点元素集合。
     * @param xpath
     * @return List
     * @throws XMLException
     */
    public List getAllElements(String xpath) throws XMLException
    {
        List elements = null;
        try
        {
            elements = XPath.selectNodes(doc, xpath);
        }
        catch (JDOMException e)
        {
            if (log.isErrorEnabled())
            {
                log.error("获取节点元素集合发生异常!" + e.getMessage());
            }
            throw new XMLException(e.getMessage());
        }
        return elements;
    }

    /**
     * 返回满足XPath条件的第一个节点元素。
     * @param xpath
     * @return Element
     * @throws XMLException
     */
    public Element getSingleElement(String xpath) throws XMLException
    {
        //所有查询单个元素的方法都调用了这个方法,所以只在这里使用cache
        if (lookupCache.containsKey(xpath))
        {
            return (Element) lookupCache.get(xpath);
        }
        else
        {
            Element element = null;
            try
            {
                element = (Element) XPath.selectSingleNode(doc, xpath);
            }
            catch (JDOMException e)
            {
                if (log.isErrorEnabled())
                {
                    log.error("获取节点元素发生异常!" + e.getMessage());
                }
                throw new XMLException(e.getMessage());
            }
            lookupCache.put(xpath, element);
            return element;
        }
    }

    /**
     * 返回满足XPath条件的第一个节点元素的内容,字符串格式
     * @param xpath
     * @return String。如果指定的元素不存在,返回null。
     * @throws XMLException
     */
    public String getSingleElementText(String xpath) throws XMLException
    {
        Element element = getSingleElement(xpath);
        if (element == null)
        {
            return null;
        }
        else
        {
            return element.getTextTrim();
        }
    }

    /**
     * 返回满足XPath条件的第一个节点元素的指定属性。
     * @param xpath
     * @param attrName
     * @return Attribute
     * @throws XMLException
     */
    public Attribute getElementAttribute(String xpath, String attrName)
        throws XMLException
    {
        if (getSingleElement(xpath) == null)
        {
            return null;
        }
        else
        {
            return getSingleElement(xpath).getAttribute(attrName);
        }
    }

    /**
     * 返回满足XPath条件的第一个节点元素的指定属性的内容值。
     * @param xpath
     * @param attrName
     * @return String 属性的内容值,如果指定的属性不存在,返回null
     * @throws XMLException
     */
    public String getElementAttributeValue(String xpath, String attrName)
        throws XMLException
    {
        Attribute attr = getElementAttribute(xpath, attrName);
        if (attr == null)
        {
            return null;
        }
        else
        {
            return attr.getValue().trim();
        }
    }

    /**
     * 在指定的元素下面增加一个元素。
     * @param xpath 指定的元素
     * @param elemName 增加元素的名称
     * @param elemText 增加元素的内容
     * @throws XMLException
     */
    public void addElement(String xpath, String elemName, String elemText)
        throws XMLException
    {
        Element parent = getSingleElement(xpath);
        parent.addContent(new Element(elemName).addContent(elemText));
    }

    /**
     * 使指定位置的元素从他的上级脱离。并且返回这个元素。如果没有上级,不作任何删除
     * 的操作。
     * @param xpath
     * @return 被修改的元素
     * @throws XMLException
     */
    public Content removeElement(String xpath) throws XMLException
    {
        lookupCache.remove(xpath);
        Element element = getSingleElement(xpath);
        if (element.isRootElement())
        {
            return element;
        }
        else
        {
            return element.detach();
        }
    }

    /**
     * 改变指定元素的文本内容。
     * @param xpath 指定元素
     * @param elemText 需要设置的文本
     * @throws XMLException 如果指定的元素不存在
     */
    public void setElementText(String xpath, String elemText)
        throws XMLException
    {
        Element element = getSingleElement(xpath);
        if (element == null)
        {
            throw new XMLException("指定的元素不存在!");
        }
        else
        {
            element.setText(elemText);
        }
    }

    /**
     * 在指定路径的元素上增加一个属性。如果同名属性已经存在,重新设置这个属性的值。
     * @param xpath
     * @param attrName
     * @param attrValue
     * @throws XMLException
     */
    public void setAttribute(String xpath, String attrName, String attrValue)
        throws XMLException
    {
        Element element = getSingleElement(xpath);
        try
        {
            element.setAttribute(attrName, attrValue);
        }
        catch (Exception e)
        {
            if (log.isErrorEnabled())
            {
                log.error("设置节点元素的属性发生异常!" + e.getMessage());
            }
            throw new XMLException(e.getMessage());
        }
    }

    /**
     * 删除指定元素的指定属性。
     * @param xpath
     * @param attrName
     * @return boolean
     * @throws XMLException
     */
    public boolean removeAttribute(String xpath, String attrName)
        throws XMLException
    {
        Element element = getSingleElement(xpath);
        if (element == null)
        {
            return false;
        }
        else
        {
            return element.removeAttribute(attrName);
        }
    }
}

 

 

/*
 * Created on 2003-6-5
 */
package accp.util.xml;

/**
 * @author David Yu
 */
public class XMLException extends Exception
{

    /**
     * @param mgs
     */
    public XMLException(String mgs)
    {
        super(mgs);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值