你可以使用JDOM包解决这个问题,我这里有详细的源代码,你给我E-mail我发给你,是自己写的,很简单一看就应该明白。

解决方案 »

  1.   

    package util.common;import java.io.*;
    import java.util.*;
    import org.jdom.*;
    import org.jdom.input.*;
    import org.jdom.output.*;
    public class XMLProperties
    {
        private boolean autoSave = true;
        private File file;
        private Document doc;
        private Map propertyCache = new HashMap();    public XMLProperties(String file, boolean autoSave, boolean validate)
                throws JDOMException
        {
            this.autoSave = autoSave;
            this.file = new File(file);
            SAXBuilder builder = new SAXBuilder(validate);
            try
            {
                doc = builder.build(file);
            }
            catch (JDOMException ex)
            {
                throw new JDOMException("构造过程出现错误! " + ex);
            }
        }    public XMLProperties(String file, boolean autoSave)   throws JDOMException
        {
            this(file, autoSave, false);
        }    public XMLProperties(String file)   throws JDOMException
        {
            this(file, false, false);
        }
        
        public boolean isAutoSave() {  return autoSave; }    public void setAutoSave(boolean autoSave)   {   this.autoSave = autoSave;   }    public synchronized void saveProperties() throws IOException
        {
            if(autoSave == false)   return;        XMLOutputter out = new XMLOutputter("    ",true, "GB2312");
            File temp = null;
            FileWriter writer = null;
            boolean isError = false;
            try
            {
                //先生成一个临时文件
                temp = new File(file.getParent(), file.getName() + ".tmp");
                writer = new FileWriter(temp);
                out.output(doc, writer);
            }
            catch (IOException ex)
            {
                isError = true;
                throw new IOException("生成临时文件出现错误!" + ex);
            }
            finally
            {
                writer.close();
            }
            //备份源文件,生成新文件
            if(isError == false)
            {
                File bak = new File(file.getParent(), file.getName() + ".bak");
                //删除原来的备份文件
                if(bak.exists())    bak.delete();
                boolean isSuccess = file.renameTo(bak);
                file.delete();
                //临时文件变成正式文件
                isSuccess = temp.renameTo(file);
                if(isSuccess == false)
                    throw new IOException("备份文件过程出现错误!");
            }
        }    public String getPorperty(String name)
        {
            if(propertyCache.containsKey(name))
                return propertyCache.get(name).toString();        //查找指定的元素元素
            Element element = this.findOnly(name);
            if(element == null) return null;        String value = element.getTextTrim();
            propertyCache.put(name, value);
            return value;
        }    public String getPorperty(String name, String attr)
        {
            String nameAttr = name + ":" + attr;
            if(propertyCache.containsKey(nameAttr))
                return propertyCache.get(nameAttr).toString();        //查找指定的元素元素
            Element element = this.findOnly(name);
            if(element == null) return null;        String value = element.getAttributeValue(attr);
            propertyCache.put(nameAttr, value);
            return value;
        }    public void setProperty(String name, String value) throws IOException
        {
            propertyCache.put(name, value);        //查找指定的元素元素
            Element element = this.findCreate(name);
            element.setText(value);
            saveProperties();
        }
        
        public void setProperty(String name, String attr, String value) throws IOException
        {
            String nameAttr = name + ":" + attr;
            propertyCache.put(nameAttr, value);        //查找指定的元素元素
            Element element = this.findCreate(name);
            element.setAttribute(attr, value);
            saveProperties();
        }    public void deleteProperty(String name) throws IOException
        {
            if(propertyCache.containsKey(name))
                propertyCache.remove(name);        Element element = this.findOnly(name);
            if(element != null)  element.detach();
            saveProperties();
        }    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();
        }    public String[] getChildrenProperties(String parent)
        {
            //分解元素的名称
            String[] propName = parsePropertyName(parent);
            Element element = doc.getRootElement();
            //遍历搜索匹配的元素
            for (int i = 0; i < propName.length; i++)
            {
                element = element.getChild(propName[i]);
                //没有匹配的元素,返回一个空的数组
                if(element == null) return new String[]{};
            }
            //找到啦!
            List children = element.getChildren();
            String[] childrenName = new String[children.size()];
            for (int i = 0; i < children.size(); i++)
            {
                childrenName[i] = ((Element)children.get(i)).getName();
            }
            return childrenName;
        }    private String[] parsePropertyName(String name)
        {
            //因为符号"."是保留字符,所以不能使用String.split()
            //所以先进行一次替代
            return name.replace('.','#').split("#");
        }
        
        private Element findOnly(String name)
        {
            //分解元素的名称
            String[] propName = parsePropertyName(name);
            Element element = this.doc.getRootElement();
            //遍历搜索匹配的元素
            for (int i = 0; i < propName.length; i++)
            {
                element = element.getChild(propName[i]);
                if(element == null) return null;
            }
            //找到啦!
            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;
        }
    }