且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用java从xml文件中读取属性?

更新时间:2023-01-18 18:43:49

这很简单,假设您愿意将属性文件重写为标准 Java 格式.假设您在名为 props.xml 的文件中有以下内容:

This is trivial, assuming you're willing to re-write your properties file into the standard Java format. Assume you have the following in a file called props.xml:

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <comment>This is a comment</comment>
    <entry key="propA">A</entry>
    <entry key="propB">B</entry>
    <entry key="propC">C</entry>
    <entry key="propD">D</entry>
    <entry key="propE">E</entry>
    <entry key="propF">F</entry>
</properties>

然后像这样从文件中读取属性:

Then read properties from the file like this:

java.util.Properties prop = new Properties();
prop.loadFromXML(new FileInputStream("props.xml"));
System.out.println(prop.getProperty("propF"));