<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      搜集整理的對xml文件設置的java程序,基本可以滿足基本的設置。

      [摘要]包括生成xml文件,增加刪除修改節點,增加修改屬性等等,F把我的程序共享,還有我的測試例子,看看大家有什么更好的建議。其中用到了jdom.jar 和xerces-1.2.2.jar ,都是標準的xm...
      包括生成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)
      {
      // 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();
      }


      日韩精品一区二区三区高清