欢迎光临
我们一直在努力

搜集整理的对xml文件操作的java程序,基本可以满足基本的操作。-JSP教程,Java与XML

建站超值云服务器,限时71元/月

包括生成xml文件,增加删除修改节点,增加修改属性等等。
现把我的程序共享,还有我的测试例子,看看大家有什么更好的建议。
其中用到了jdom.jar 和xerces-1.2.2.jar ,都是标准的xml解析包。
可以从网上下载。
/**
* $rcsfile: xmlproperty.java,v $
* $revision: 1.3 $
* $date: 2002/04/02 21:17:09 $
*
* copyright (c) 1999-2001 coolservlets, inc. all rights reserved.
*
* this software is the proprietary information of coolservlets, inc.
* use is subject to license terms.
*/

package crm.bean.common;

import java.io.*;
import java.util.*;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.xml.sax.*;

/**
* provides the the ability to use simple xml property files. each property is
* in the form x.y.z, which would map to an xml snippet of:
* <pre>
* <x>
*     <y>
*         <z>somevalue</z>
*     </y>
* </x>
* </pre>
*
* the xml file is passed in to the constructor and must be readable and
* writtable. setting property values will automatically persist those value
* to disk.
*/
public class xmlproperty {

    private string xfile = null;
    private file file;
    private document doc;
    private string pgid="xmlproperty.java";
    /**
     * parsing the xml file every time we need a property is slow. therefore,
     * we use a map to cache property values that are accessed more than once.
     */
    private map propertycache = new hashmap();

    /**
     * creates a new xmlproperty object.
     *
     * @param file the full path the file that properties should be read from
     *      and written to.
     */
    public xmlproperty(string file)
    {
        this.file = new file(file);
        this.xfile = file;
        try
        {
            saxbuilder builder = new saxbuilder();
            doc = builder.build(new file(file));
        }
        catch (exception e)
        {
            system.err.println("error creating xml parser in " + pgid);
            e.printstacktrace();
        }
        //
    }
    public xmlproperty(document doc)
    {
        this.doc=doc;
    }
   
    public document getdocument()
    {
        return this.doc;
    }
    /**
    * return all children property names of a parent property as a string array,
    * or an empty array if the if there are no children. for example, given
    * the properties <tt>x.y.a</tt>, <tt>x.y.b</tt>, and <tt>x.y.c</tt>, then
    * the child properties of <tt>x.y</tt> are <tt>a</tt>, <tt>b</tt>, and
    * <tt>c</tt>.
    *
    * @param parent the name of the parent property.
    * @return all child property values for the given parent.
    */
    public string [] getchildrenproperties(string parent) {
        string[] propname = parsepropertyname(parent);
        // search for this property by traversing down the xml heirarchy.
        element element = doc.getrootelement();
        for (int i = 0; i < propname.length; i++) {
            element = element.getchild(propname[i]);
            if (element == null) {
                // this node doesnt match this part of the property name which
                // indicates this property doesnt exist so return empty array.
                return new string [] { };
            }
        }
        // we found matching property, return names of children.
        list children = element.getchildren();
        int childcount = children.size();
        string [] childrennames = new string[childcount];
        for (int i=0; i<childcount; i++) {
            childrennames[i] = ((element)children.get(i)).getname();
        }
        return childrennames;
    }

    //qiuss 2001.11.08
    public list getchildren(string name)
    {
                
        string[] propname = parsepropertyname(name);
        // search for this property by traversing down the xml heirarchy.
        element element = doc.getrootelement();
                
        if(propname!=null)
        {                // qiuss    
            for (int i = 0; i < propname.length; i++)
            {
                element = element.getchild(propname[i]);
                if (element == null)
                {
                    // this node doesnt match this part of the property name which
                    // indicates this property doesnt exist so return null.
                    return null;
                }
            }
        }
        // we found matching property, return names of children.
        return(element.getchildren());
    }
    /**
    *@param     element element==null start from root
    *@param name        name==null return this elements value
    *@author qiuss
    *@date 2001.11.08
    */
    public string getproperty(element element,string name)
    {
        if (propertycache.containskey(name))
        {
            return (string)propertycache.get(name);
        }
        string[] propname = parsepropertyname(name);
        // search for this property by traversing down the xml heirarchy.
        if(element==null) //from rootelement when element equals null
            element = doc.getrootelement();
        if(propname!=null)
        {
            for (int i = 0; i < propname.length; i++)
            {
                element = element.getchild(propname[i]);
                if (element == null)
                {
                    // this node doesnt match this part of the property name which
                    // indicates this property doesnt exist so return null.
                    return null;
                }
            }
        }
        // at this point, we found a matching property, so return its value.
        // empty strings are returned as null.
        string value = element.gettext();
        if ("".equals(value)) {
            return null;
        }
        else {
            value = value.trim();
            propertycache.put(name, value);
            return value;
        }
    }
    /**
    * returns the value of the specified property.
    * @param name the name of the property to get.
    * @return the value of the specified property.
    */
    public string getproperty(string name)
    {
        return getproperty(null,name);
    }
    /**
    *@param element element==null start from root
    *@param name        childs element name eg. x.y.z
    *@param attname attribute name     eg. "abc=1234"     if attname==null get first element
    *@return searched element
    *@author qiuss
    *@date 2001.11.08
    */
    public element getchild(element element,string name,string attname)
    {
        //if(name==null) return element;
        string[] propname = parsepropertyname(name);
        string[] attrname = parseattributename(attname);
             
        // search for this property by traversing down the xml heirarchy.
        if(element==null)
            element = doc.getrootelement();
        if(propname!=null)
        {
            // qiuss    
            for (int i = 0; i < propname.length; i++)
            {
                element = element.getchild(propname[i]);
                if (element == null)
                {
                    // this node doesnt match this part of the property name which
                    // indicates this property doesnt exist so return null.
                    return null;
                }
            }
        }
        // we found matching property, return names of children.
        list elist=element.getchildren();
        if(attrname==null)
            return (element)elist.get(0);
        iterator it=elist.iterator();
        while(it.hasnext()){
            attribute att;
            element ele=(element)it.next();
            if((att=ele.getattribute(attrname[0]))!=null)
            {
                if(att.getvalue().equals(attrname[1]))
                    return ele;
            }
        }
        return null;
    }  
    /**
     * 设置一个元素值。如果指定的元素不存在,就自动创建该元素。
     *
     * @param name 需要赋值的元素名称,格式为 x.y.z
     * @param value 元素值
     *
     * @throws ioexception
     */
    public void setproperty(string name, string value)
    {
        propertycache.put(name, value);

        //查找指定的元素元素
        element element = this.findcreate(name);
        element.settext(value);
        saveproperties();
    }

    /**
     * 设置一个元素的attribute值。如果指定的元素不存在,就自动创建该元素。
     *
     * @param name 需要赋值的元素名称,格式为 x.y.z
     * @param attr attribute名称
     * @param value 元素值
     *
     * @throws ioexception
     */
    public void setproperty(string name, string attr, string value)
    {
        string nameattr = name + ":" + attr;
        propertycache.put(nameattr, value);

        //查找指定的元素元素
        element element = this.findcreate(name);
        element.setattribute(attr, value);
        saveproperties();
    }
    /**
     * helper方法,查找一个指定的元素,如果这个元素不存在,创建它
     *
     * @param name 元素名称,格式为 x.y.z
     * @return element 找到就返回这个元素,否则返回创建的新元素
     */
    private element findcreate(string name)
    {
        //分解元素的名称
        string[] propname = parsepropertyname(name);
        element element = doc.getrootelement();
        //遍历搜索匹配的元素,不存在则创建它
        for (int i = 0; i < propname.length; i++)
        {
            if(element.getchild(propname[i]) == null)
            {
                //自动创建该元素
                element.addcontent(new element(propname[i]));
            }
            element = element.getchild(propname[i]);
        }
        //找到啦!
        return element;
    }

   /**
     * 删除指定的元素,如果该元素不存在,不做任何事情
     *
     * @param name 要删除的元素的名称,格式为 x.y.z
     *
     */
    public void deleteproperty(string name)
    {
        if(propertycache.containskey(name))
            propertycache.remove(name);

        element element = this.findonly(name);
        if(element != null)  element.detach();
        saveproperties();
    }

    /**
     * 删除指定的元素的attribute,如果该元素或者attribute不存在,不做任何事情
     *
     * @param name 元素的名称,格式为 x.y.z
     * @param attr attribute的名称
     *
     * @throws ioexception
     */
    public void deleteproperty(string name, string attr) throws ioexception
    {
        string nameattr = name + ":" + attr;
        if(propertycache.containskey(nameattr))
            propertycache.remove(nameattr);

        element element = this.findonly(name);
        if(element != null) element.removeattribute(attr);
        saveproperties();
    }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 搜集整理的对xml文件操作的java程序,基本可以满足基本的操作。-JSP教程,Java与XML
分享到: 更多 (0)

相关推荐

  • 暂无文章