包括生成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: * <div> * <X> * <Y> * <Z>someValue</Z> * </Y> * </X> * </div> * * 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) { // This node doesn't match this part of the property name which // indicates this property doesn't 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 namename==null return this element's 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 doesn't match this part of the property name which // indicates this property doesn't 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 namechild's 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 doesn't match this part of the property name which // indicates this property doesn't 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(); } |