Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将XML转换为等效的groovy配置_Xml_Groovy_Configuration Files - Fatal编程技术网

将XML转换为等效的groovy配置

将XML转换为等效的groovy配置,xml,groovy,configuration-files,Xml,Groovy,Configuration Files,我在XML文档中有一些测试配置,如下所示: <WapRules> <WapRule> <RuleTypeId>1</RuleTypeId> <IsExact>false</IsExact> <Keywords> <Keyword>gmail</Keyword> </Keywords>

我在XML文档中有一些测试配置,如下所示:

<WapRules>
    <WapRule>
        <RuleTypeId>1</RuleTypeId>
        <IsExact>false</IsExact>
        <Keywords>
            <Keyword>gmail</Keyword>
        </Keywords>
    </WapRule>
    <WapRule>
        <RuleTypeId>2</RuleTypeId>
        <IsExact>false</IsExact>
        <Keywords>
            <Keyword>test</Keyword>
        </Keywords>
    </WapRule>
    <WapRule>
        <RuleTypeId>2</RuleTypeId>
        <IsExact>true</IsExact>
        <Keywords>
            <Keyword>srs</Keyword>
            <Keyword>sample</Keyword>
        </Keywords>
    </WapRule>
</WapRules>
wap_rules = [
    {
        rule_type_id = 1
        is_exact = false
        keywords = ["gmail"]
    },
    {
        rule_type_id = 2
        is_exact = false
        keywords = ["test"]
    },
    {
        rule_type_id = 2
        is_exact = true
        keywords = ["srs", "sample"]
    }
]
现在我在配置中咕噜咕噜地说,并尝试访问元素:

def cfg = new ConfigSlurper().parse(Config)
println cfg.wap_rules[0].rule_type_id
但是输出只有以下内容:
[:]


那么如何访问cfg.wap_rules[0]中的成员呢?我将XML结构映射到Config.groovy是否有问题?

得到了答案。我没有正确映射XML文档

下面是我应该如何编写Config.groovy的:

wap_rules = [
    [
        rule_type_id : 1,
        is_exact : false,
        keywords : ["gmail"]
    ],
    [
        rule_type_id : 2,
        is_exact : false,
        keywords : ["test"]
    ],
    [
        rule_type_id : 2,
        is_exact : true,
        keywords : ["srs", "sample"]
    ]

]
wap_rules
现在是一个地图列表,每个地图的元素都可以轻松访问

现在我确实可以做
println cfg.wap\u rules[0]。rule\u type\u id
并且输出确实是
1

我想当我编写错误的Config.groovy时,我太习惯于JSON表示(即“我需要一个JSON对象列表,每个对象都用大括号{}”)了

我希望这对其他人有帮助