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>
<!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;
* @(#)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;
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;
* 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
*
*/
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
*
*/
public XmlElement(String name) {
this.name = name;
this.attributes = new Hashtable<String, String>(10);
subElements = new Vector<XmlElement>();
data = "";
}