从xml中的.properties调用路径

时间:2013-03-21 15:00:57

标签: java xml

我有一个调用路径的xml,位置是硬编码的。

<path=c:/... />

有谁知道如何使用.properties放入路径然后从那里调用它? 我出于安全原因这样做。

修改

遵循教程 http://www.mulesoft.org/documentation/display/current/Basic+Studio+Tutorial

入站的路径调用如下:

<file:inbound-endpoint  path="C: ../>

可以从属性文件中加载吗?

2 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <comment>Application Configuration</comment>
  <entry key="data.folder">D:\App\Data</entry>
  <entry key="jdbc.url">jdbc:mysql://localhost/mydb</entry>
</properties>

LoadXmlProperties .java

import java.io.FileInputStream;
import java.util.Properties;

public class LoadXmlProperties {
    public static void main(String[] args) {
        LoadXmlProperties lxp = new LoadXmlProperties();
        try {
            Properties properties = lxp.readProperties();
            /*
             * Display all properties information
             */
            properties.list(System.out);

            /*
             * Read the value of data.folder and jdbc.url configuration
             */
            String dataFolder = properties.getProperty("data.folder");
            System.out.println("dataFolder = " + dataFolder);
            String jdbcUrl = properties.getProperty("jdbc.url");
            System.out.println("jdbcUrl = " + jdbcUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Properties readProperties() throws Exception {
        Properties properties = new Properties();
        FileInputStream fis = new FileInputStream("configuration.xml");
        properties.loadFromXML(fis);

        return properties;
    }
}

答案 1 :(得分:0)

加载属性文件的代码是:

    Properties prop = new Properties();
    prop.load(new FileInputStream("C:\\prop.properties"));
    String path = prop.getProperty("pathname");

但是你有了属性文件的硬编码路径: 你可以把路径作为主要方法的arg传递:

public static void main(String Args[]){ 
    try {
        if(Args != null){
            if(Args[1] != null){//Load XML or prop File                 try{
                    properties.load(new FileInputStream(Args[1]));
                }catch(Exception e){
                    throw new Exception("Error loading PropertieFile: "+Args[1] + " :"+e.getMessage());
                }
            }else{
                throw new Exception("No File Found!");
            }