简体   繁体   English

解析自定义xml配置文件

[英]Parsing custom xml config file

My target is java 1.5 I have a custom configuration file provided from another software provider i need to read it 我的目标是Java 1.5我有另一个软件提供商提供的自定义配置文件,我需要阅读它

<?xml version="1.0" encoding="ISO-8859-1"?>
<environments>
    <environment key="DEFAULT" description="Default">
        <variable name="LOGGER_NAME" value="LCI"/>
        <variable name="MAIL_SERVER" value="127.0.0.1"/>
            ......
    </environment>
    <environment key="TEST" description="Test">
        <variable name="LOGGER_NAME" value="LCO"/>
        <variable name="MAIL_SERVER" value="192.168.2.15"/>
            ......
    </environment>
</environments>

I need to put it in a hash map and acces to it as. 我需要将其放在哈希图中,并按其访问。

MyPropertyManager.getProperty("DEFAULT","LOGGER_NAME") MyPropertyManager.getProperty( “默认”, “LOGGER_NAME”)

I think that I can load infos in a HashMap where I can acces usingg key like DEFAULT.LOGGER_NAME 我认为我可以在HashMap中加载信息,在其中我可以使用DEFAULT.LOGGER_NAME之类的g键访问

Can I use APACHE COMMONS Configuration (HOW?) or is too complex and is better to use Xpath? 我可以使用APACHE COMMONS配置(HOW?),还是过于复杂,最好使用Xpath?

The best solution for short config files is to build hash map. 短配置文件的最佳解决方案是构建哈希图。

XPathFactory xPathfactory = XPathFactory.newInstance();
            XPath xpath = xPathfactory.newXPath();
            XPathExpression expr1 = xpath.compile("/environments/environment");
            XPathExpression expr2 = xpath.compile("@key");
            XPathExpression expr12 = xpath.compile("variable");
            XPathExpression expr121 = xpath.compile("@name");
            XPathExpression expr122 = xpath.compile("@value");

            NodeList environmentNL = (NodeList) expr1.evaluate(doc, XPathConstants.NODESET);
            for (int i = 0; i < environmentNL.getLength(); i++) {
                Node environmentI = environmentNL.item(i);
                String envKey =  (String) expr2.evaluate(environmentI, XPathConstants.STRING);


                NodeList variableNL = (NodeList) expr12.evaluate(environmentI, XPathConstants.NODESET);
                for (int j = 0; j < variableNL.getLength(); j++) {
                    Node variableI = variableNL.item(j);
                    String valueName =  (String) expr121.evaluate(variableI, XPathConstants.STRING);
                    String valueValue =  (String) expr122.evaluate(variableI, XPathConstants.STRING);

                    val.put(envKey+"."+valueName, valueValue);

                }
            }

And add two methods In this case I use the variable namespace instead environment 并添加两个方法在这种情况下,我使用变量名称空间代替环境

1) In this case 1)在这种情况下

public static String getProperties(String namespace, String value) throws ConfigLoaderException {
        String param=namespace+"."+value;

        if(!val.containsKey(param)){
            throw new ConfigLoaderException(param+" ERROR");
        }else{
            return val.get(param);

        }
    }

... ...

public static String getProperties(String value) throws ConfigLoaderException {
        String namespace="DEFAULT";
        return getProperties(namespace,value);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM