以sample1.xml文件为例,我们来看看如何将“磁盘disk2”的容量加大变为20G。
<?xml version="1.0" encoding="UTF-8"?>
<HD>
<disk1>
<capacity>8G</capacity>
<directories>200</directories>
<files>1580</files>
</disk1>
<disk2>
<capacity>10G</capacity>
<directories>500</directories>
<files>3000</files>
</disk2>
</HD>
jive中有一个非常出名的类,XMLProperties.java,它提供了对XML数据源的增 修 删等功能,基本的方法就是一级一级的从根元素到子元素递归找到子元素,不管这个子元素有多深,我们都能够设置、修改或删除它。
首先,我们用"."来分隔从根到子元素的路径(不包含根)disk.capacity,然后我们就可以用XMLProperties类对象的方法setProperty(String name, String value)来将“磁盘disk2”的容量加大变为20G。
public class Test3{
public static void main(String args[]){
XMLProperties xp=new XMLProperties("sample1.xml");
System.out.println(xp.getProperty("disk2.capacity"));
xp.setProperty("disk2.capacity","20G");
System.out.println("OK");
System.out.println(xp.getProperty("disk2.capacity"));
}
}
运行结果:
C:/java>java Test3
10G
OK
20G
最后请看:XMLProperties.java(来自jivejdon2.6,请用它带的jdom.jar编译运行)
import java.io.*;
import java.text.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class XMLProperties {
private File file;
private Document doc;
private Map propertyCache = new HashMap();
public XMLProperties(String file) {
this.file = new File(file);
try {
SAXBuilder builder = new SAXBuilder();
// DataUnformatFilter format = new DataUnformatFilter();
// builder.setXMLFilter(format);
doc = builder.build(new File(file));
}
catch (Exception e) {
System.err.println("Error creating XML parser in "
+ "PropertyManager.java");
e.printStackTrace();
}
}
public String getProperty(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 {
value = value.trim();
propertyCache.put(name, value);
return value;
}
}
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;
}
public void setProperty(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);
saveProperties();
}
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]);
saveProperties();
}
private synchronized void saveProperties() {
OutputStream out = null;
boolean error = false;
File tempFile = null;
try {
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
XMLOutputter outputter = new XMLOutputter(" ", true);
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);
}
}
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;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<HD>
<disk1>
<capacity>8G</capacity>
<directories>200</directories>
<files>1580</files>
</disk1>
<disk2>
<capacity>10G</capacity>
<directories>500</directories>
<files>3000</files>
</disk2>
</HD>
jive中有一个非常出名的类,XMLProperties.java,它提供了对XML数据源的增 修 删等功能,基本的方法就是一级一级的从根元素到子元素递归找到子元素,不管这个子元素有多深,我们都能够设置、修改或删除它。
首先,我们用"."来分隔从根到子元素的路径(不包含根)disk.capacity,然后我们就可以用XMLProperties类对象的方法setProperty(String name, String value)来将“磁盘disk2”的容量加大变为20G。
public class Test3{
public static void main(String args[]){
XMLProperties xp=new XMLProperties("sample1.xml");
System.out.println(xp.getProperty("disk2.capacity"));
xp.setProperty("disk2.capacity","20G");
System.out.println("OK");
System.out.println(xp.getProperty("disk2.capacity"));
}
}
运行结果:
C:/java>java Test3
10G
OK
20G
最后请看:XMLProperties.java(来自jivejdon2.6,请用它带的jdom.jar编译运行)
import java.io.*;
import java.text.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class XMLProperties {
private File file;
private Document doc;
private Map propertyCache = new HashMap();
public XMLProperties(String file) {
this.file = new File(file);
try {
SAXBuilder builder = new SAXBuilder();
// DataUnformatFilter format = new DataUnformatFilter();
// builder.setXMLFilter(format);
doc = builder.build(new File(file));
}
catch (Exception e) {
System.err.println("Error creating XML parser in "
+ "PropertyManager.java");
e.printStackTrace();
}
}
public String getProperty(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 {
value = value.trim();
propertyCache.put(name, value);
return value;
}
}
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;
}
public void setProperty(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);
saveProperties();
}
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]);
saveProperties();
}
private synchronized void saveProperties() {
OutputStream out = null;
boolean error = false;
File tempFile = null;
try {
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
XMLOutputter outputter = new XMLOutputter(" ", true);
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);
}
}
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;
}
}