用DOM/JDOM解析XML文件(2)

      很久没有发贴了,似乎已经彻底遗忘了这个角落。也许,只有在心情低落的时候才会想起,只有我的blog能陪我彻夜无眠。就像一只受伤的动物,总有一个秘密的藏身之处,与世隔绝,只有自己知道,在这个私人的角落,有梦想、有回忆、有失落、也有甜蜜……

      上次的文章末尾提到要补一个同样功能的JDom版XML解析器,找了半天,似乎丢了,勉强看到一个,也不知道是不是当时的最终版本,乍一看上去,应该差不多的,就拿来充充数吧。我想我是彻底的堕落了,竟然要这么长时间才能续写一篇早已承诺的文字。要不是今夜无眠,不知又要推到什么时候了呢。

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
* <p>ConfigParser</p>
* 1. read the config XML,or update the file.</br>
* 2. query a property from the config file,the property name fromat:A.B.C etc.</br>
* 3. add properties into the config file.</br>
*
* @author javer QQ:84831612
*/
public class ConfigParser
{
   private File file;
   private Document doc;

   private Map propertyCache = new HashMap();

   public ConfigParser(String filePath)
   {
      this.file = new File(filePath);
      try {
      SAXBuilder builder = new SAXBuilder();
      doc = builder.build(new File(filePath));
   }
   catch (Exception e) {
      System.err.println("Error creating XML parser in ConfigParser.java");
      e.printStackTrace();
   }
}

/**
* 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);
   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();
   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;
}

/**
* Return the value of the specified property.
*
* @param name the name of the property to get.
* @return the value of the specified property.
*/
public String getValue(String name)
{
   if (propertyCache.containsKey(name)) {
      return (String)propertyCache.get(name);
   }

   String[] propName = parsePropertyName(name);
   Element element = doc.getRootElement();
   for (int i = 0; i < propName.length; i++) {
      element = element.getChild(propName[i]);
      if (element == null) {
         return null;
      }
   }

   String value = element.getText();
   if ("".equals(value)) {
      return null;
   }
   else {
      propertyCache.put(name, value);
      return value;
   }
}

/**
* Return the value when the specified property key and value is existent,
* and return null if the specified property key and value is not existent
*
* @param name
* @param value
* @return
*/
public String findValue(String name,String value)
{
   String[] propName = parsePropertyName(name);

   Element element = doc.getRootElement();
   return findValue(element,0,propName,value);
}

private String findValue(Element element, int depth, String[] propName, String value)
{
   String _value = null;
   if(depth == propName.length)
   {
      _value = element.getText();
      if (_value.equals(value))
         return value;
      else
         return null;
   }

   List children = element.getChildren(propName[depth]);
   if(children==null || children.size()<1)
      return null;
   else
      ++depth;

   for(int i=0;i<children.size();i++)
   {
      element = (Element)children.get(i);
      _value = findValue(element,depth,propName,value);
      if(_value != null)
      {
         return value;
      }
   }

   return null;
}

/**
* Update the value of the specified property. If the property doesn't
* currently exist, it will be automatically created.
* @param name
* @param value
*/
public void updateValue(String name,String value)
{
   propertyCache.put(name, value);

   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]);
   }

   element.setText(value);
   store();
}

/**
* <p>Add the values by the specified properties.</p>
* For example:</br>
* if values is existed as follow</br>
* x.y.z1=1</br>
* .z2=2</br>
* .z3=3</br>
* and now, you need add new values as a new group,like as follow values</br>
* x.y.z1=11</br>
* .z2=22</br>
* .z3=33</br>
* you need call this function like as follow:</br>
* addValue("x.y",new String[]{"z1","z2","z3"},new String[]{"11","22","33"})</br>
* and the XML will have a result as follow:</br>
* <xml>
* <x>
* <y>
* <z1>1</z1>
* <z2>2</z2>
* <z2>3</z2>
* </y>
* </x>
* <x>
* <y>
* <z1>11</z1>
* <z2>22</z2>
* <z2>33</z2>
* </y>
* </x>
* </xml>
*
*
* @param parentName
* @param subNames
* @param values
*/
public void addValue(String parentName,String[] subNames,String[] values)
{
   String[] propName = parsePropertyName(parentName);

   Element element = doc.getRootElement();
   for (int i=0; i<propName.length-1; i++) {
      if (element.getChild(propName[i]) == null) {
         element.addContent(new Element(propName[i]));
      }
      element = element.getChild(propName[i]);
   }

   Element parentElement = new Element(propName[propName.length-1]);
   element.addContent(parentElement);

   for(int i=0;i<subNames.length;i++)
   {
      element = new Element(subNames[i]);
      element.setText(values[i]);
      parentElement.addContent(element);
   }

   store();
}

/**
* Deletes the specified property.
*
* @param name the property to delete.
*/
public void deleteProperty(String name) {
   String[] propName = parsePropertyName(name);
   Element element = doc.getRootElement();
   for (int i=0; i<propName.length-1; i++) {
      element = element.getChild(propName[i]);
      if (element == null) {
         return;
      }
   }
   element.removeChild(propName[propName.length-1]);
   store();
}

private synchronized void store()
{
   OutputStream out = null;
   boolean error = false;
   File tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
   try {
      XMLOutputter outputter = new XMLOutputter();
      out = new BufferedOutputStream(new FileOutputStream(tempFile));
      outputter.output(doc, out);
   }
   catch (Exception e) {
      e.printStackTrace();
      error = true;
   }
   finally {
      try { out.close(); }
      catch (Exception e) {
         e.printStackTrace();
         error = true;
      }
   }

   if (!error) {
      file.delete();
      tempFile.renameTo(file);
   }
}

/**
* Returns an array representation of the given Lss config property. Lss config
* properties are always in the format "prop.name.is.this" which would be
* represented as an array of four Strings.
*
* @param name the name of the Lss Config property.
* @return an array representation of the given Lss Config property.
*/
private String[] parsePropertyName(String name) {
   int size = 1;
   for (int i=0; i<name.length(); i++) {
      if (name.charAt(i) == '.') {
         size++;
      }
   }
   String[] propName = new String[size];
   StringTokenizer tokenizer = new StringTokenizer(name, ".");
   int i = 0;
   while (tokenizer.hasMoreTokens()) {
      propName[i] = tokenizer.nextToken();
      i++;
   }
   return propName;
}
}

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值