如何在apache常见的情况下使用XMLConfigurtion检查节点是否存在?

时间:2012-02-20 03:27:19

标签: java apache configuration apache-commons

<?xml version="1.0" encoding="utf-8"?>
<processor>
    <user_config>
        <a>xxxxx</a>    
    </user_config>
</processor>

我想检查这个xml配置文件中是否存在user_config,是否有任何方法可以在org.apache.common.XMLConfiguration中使用?

3 个答案:

答案 0 :(得分:3)

我用以下方法解决了问题:

if(configuration.configurationsAt( "user_config" ).size() > 0 ) {
    //it exists
}

我不喜欢这个解决方案。如果有人知道更好的解决方案 - &gt;请分享。

答案 1 :(得分:0)

虽然我不确定XMLConfiguration,但您可以使用org.apache.xpath来检查user_config是否存在,或者从字符串,输入流或文件中将其解析为DOM对象并检查它。

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("");

答案 2 :(得分:0)