当前位置:编程学习 > JSP >>

搜集整理的对xml文件操作的java程序,基本可以满足基本的操作。

答案:包括生成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 doesn't match this part of the property name which
                // indicates this property doesn't 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)
      &n

上一个:xml文件操作的java程序(续)
下一个:Java XML教程(1-3章)

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,