当前页面: 开发资料首页 → Java 专题 → 从XML中加载属性
从XML中加载属性
摘要: 从XML中加载属性
</td>
</tr>
<tr>
<td height="35" valign="top" class="ArticleTeitle">
<table width="673" border="0">
<tr>
<td width="396"> JDK1.5(代号Tiger)中更新了java.util.Properties类,提供了从XML文件中读写key-value对属性的简单方法:loadFromXML()和storeToXML()
1、基本加载属性的方法
Sample属性文件:sample.properties
foo=bar
fu=baz
加载属性的Sample程序
</td>
<td width="267">
</td>
</tr>
</table>
import java.io.FileInputStream;
import java.util.Properties;
public class LoadSampleProperties {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("props/sample.properties");
prop.load(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
}
}
输出结果如下:
-- listing properties --
fu=baz
foo=bar
The foo property: bar
2、从XML中加载属性
下面是Properties DTD清单:
<?xml version="1.0" encoding="UTF-8"?>
Sample XML属性文件:sample.xml(符合上面的Properties DTD)
<?xml version="1.0" encoding="UTF-8"?>
properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
Hello
bar
baz
标记指定具体一个属性,由key属性指定属性名,而标记的内容指定属性值
标记可以用来指定注释
从XML文件加载属性的Sample程序
import java.io.FileInputStream;
import java.util.Properties;
public class LoadSampleXML {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("props/sample.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
}
}
输出的结果是一样的
可以看出方法很简单:使用XML文件来保存属性,使用loadFromXML()方法替代原来的load()方法来加载XML文件中属性
3、更新XML文件中的属性值
Sample程序
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class UpdateSampleXml {
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("props/sample.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
prop.setProperty("foo", "Hello World!");
prop.setProperty("new-name", "new-value");
FileOutputStream fos = new FileOutputStream("props/sample.xml");
prop.storeToXML(fos, "Store Sample");
fos.close();
fis = new FileInputStream("props/sample.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("\nThe foo property: " + prop.getProperty("foo"));
}
}
上面的例子加载了sample.xml中的属性,更新了foo属性的值,并新加了new-name属性,调用storeToXML()方法保存到原文件中,并改变注释内容为Store Sample
程序执行后的sample.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
Store Sample
new-value
baz
Hello World!
function TempSave(ElementID)
{
CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
function Restore(ElementID)
{
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}
</td>
</tr>
<tr>
↑返回目录
前一篇: 简明正则速查
后一篇: 在应用程序中处理jar文件