一个自写的XML读写/存取属性的Java工具类库

2008-02-23 09:25:25来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

Java 5中的Properties类现在可以使用XML存取,通过loadFromXML和storeToXML方法实现。假设有下面这个属性表:
Windowsize: 400,400
windowLocation: 456,300
使用storeToXML后会得到这样的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Comment</comment>
<entry key="windowLocation">400,400</entry>
<entry key="windowSize">456,300</entry>
</properties>
但是如果要获得更具层次感的属性文件,可以使用这里我写的一个Utility。它建立在一个读取和存储XML的类库上。这个类库采集于Columba Project的util包,并有所修改。
首先是XmlElement,用于表示XML文件里的一个entry
/*
* @(#)XmlElement.java
* Created on 2005-8-12
*/
package com.allenstudio.ir.util;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Vector;
/**
* The XmlElement is a generic containment class for elements within an XML
* file.
* <p>
*
* It extends Observable which should be used for gui elements which are
* interested in configuration changes.
* <p>
*
* Show interested in:
*
* <pre>
* xmlElement.addObserver(yourObserver);
* </pre>
*
* <p>
* When making bigger changes on XmlElement and probably its subnodes and/or a
* greater number of attributes at once, you should just change XmlElement
* directly and manually notify the Observers by calling:
* <p>
*
* <pre>
* xmlElement.setChanged();
* xmlElement.notifyObservers();
* </pre>
*
* <p>
* There a good introduction for the Observable/Observer pattern in
* Model/View/Controller based applications at www.javaworld.com: -
* {@link http://www.javaworld.com/javaworld/jw-10-1996/jw-10-howto.html}
*
* @author fdietz
*/
public class XmlElement extends Observable implements Cloneable {
String name;
String data;
Hashtable<String, String> attributes;
List<XmlElement> subElements;
XmlElement parent;
/**
*
*
* Constructor
*
*/
public XmlElement() {
subElements = new Vector<XmlElement>();
this.attributes = new Hashtable<String, String>(10);
}
/**
* **
*
* Constructor
*
* @param String
* Name
*
*/
public XmlElement(String name) {
this.name = name;
this.attributes = new Hashtable<String, String>(10);
subElements = new Vector<XmlElement>();
data = "";
}
/**
* **
*
* Constructor
*
* @param String
* Name
* @param Hashtable
* Attributes
*
*/
public XmlElement(String name, Hashtable<String, String> attributes) {
this.name = name;
this.attributes = attributes;
subElements = new Vector<XmlElement>();
}
/**
* **
*
* Constructor
*
* @param Name
* String
* @param Data
* String
*
*/
public XmlElement(String name, String data) {
this.name = name;
this.data = data;
subElements = new Vector<XmlElement>();
this.attributes = new Hashtable<String, String>(10);
}
/**
* Add attribute to this xml element.
*
* @param name
* name of key
* @param value
* new attribute value
* @return old attribute value
*
*/
public Object addAttribute(String name, String value) {
if ((value != null) && (name != null)) {
Object returnValue = attributes.put(name, value);
return returnValue;
}
return null;
}
/**
* **
*
* @return String
* @param String
* Name
*
*/
public String getAttribute(String name) {
return ((String) attributes.get(name));
}
public String getAttribute(String name, String defaultValue) {
if (getAttribute(name) == null) {
addAttribute(name, defaultValue);
}
return getAttribute(name);
}
/**
* **
*
* @return String
* @param String
* Name

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:让Java程序带着JRE一起上路

下一篇:使用XML-RPC来访问Web服务