java - 首选项库未加载首选项

标签 java xml xml-parsing

我正在使用 java.util.prefs.Preferences 为我的应用程序创建引用,但由于某种原因,我似乎无法从 xml 文件加载首选项。我没有收到文件未找到错误,因此它正在加载文件。它似乎没有解析该文件。有人可以让我知道我做错了什么吗?

说明是的,正在加载文件。 “root.exportSubtree(System.out)”行正在输出整个 XML。然而,我从中获取的示例仅显示根“用户”的子根。这可能是对我的问题的暗示,但我仍然没有解决它。

我的首选项类

package com.g4apps.secure.processserver;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.prefs.Preferences;

public class PSPrefs {
        private String user;
        private String url;
        private String password;
        private String database;

        public PSPrefs(String file) {
            InputStream is = null;

            try {
                is = new BufferedInputStream(new FileInputStream(file));
                Preferences.importPreferences(is);
                Preferences root = Preferences.userRoot();
                root.exportSubtree(System.out);
                this.user=root.get("user", "");
                this.url=root.get("url", "");
                this.password=root.get("password","");
                this.database=root.get("database","");
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        public String getUser() {
            return this.user;
        }
        public String getURL() {
            return this.url;
        }
        public String getPassword() {
            return this.password;
        }
        public String getDatabase() {
            return this.database;
        }


}

我的preferences.xml 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
    <root type="user">
        <map/>
        <node name="Server Settings">
            <map>
                <entry key="user" value="user"/>
                <entry key="url" value="jdbc:mysql://mysqldb.com/"/>
                <entry key="password" value="password"/>
                <entry key="database" value="mydb"/>
            </map>
        </node>
    </root>
</preferences>

感谢您的所有帮助。

最佳答案

嗯,我突然想到的是该文件并不在您想象的位置。尝试使用 file.exists()

验证文件是否存在

确保文件位于执行上下文中的正确位置

更新

好吧,对不起,我的错:(

试试这个:

try {
    is = new BufferedInputStream(new FileInputStream(file));
    Preferences.importPreferences(is);
    Preferences root = Preferences.userRoot();

    Preferences node = Preferences.userRoot().node("Server Settings");

    this.user = node.get("user", "");
    this.url = node.get("url", "");
    this.password = node.get("password", "");
    this.database = node.get("database", "");

    System.out.println(user);
    System.out.println(url);
    System.out.println(password);
    System.out.println(database);

} catch (Exception e) {
    e.printStackTrace();
}

这给了我输出

user
jdbc:mysql://mysqldb.com/
password
mydb

哦,如果您使用来自 Roseindia 的示例,它实际上转储了整个首选项树,它们只是“突出显示”了那里的分支

关于java - 首选项库未加载首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11871623/

相关文章:

java - 识别 git 存储库中的参数更改

java - JAXB Marshaller 缩进

xml - Powershell,将xml输出到屏幕

Android SAX XML 解析错误

java - 萨克斯 - ExpatParser$ParseException

java - com.mysql.jdbc.MysqlDataTruncation : Data truncation: Data too long for column 'column_name'

java - 如何使用参数在 Java 中创建实例的方法?

java - 在 JButton 按下时播放音频文件?

xml - XPath - (//first//*)[1] 与//first//*[1]

c# - 我如何使用 XmlTextReader 读取保管箱上的 xml 文件