xml - 使用Groovy附加到xml文件的属性值

标签 xml grails groovy

xml的示例:

<response version-api="2.0">
    <value>
        <books>
            <book available="20" id="1" tags="">
                <title>Don Xijote</title>
                <author id="1" tags="Joel">Manuel De Cervantes</author>
            </book>
            <book available="14" id="2" tags"Jane">
                <title>Catcher in the Rye</title>
               <author id="2" tags="">JD Salinger</author>
           </book>
           <book available="13" id="3" tags="">
               <title>Alice in Wonderland</title>
               <author id="3">Lewis Carroll</author>
           </book>
           <book available="5" id="4" tags="Harry">
               <title>Don Xijote</title>
               <author id="4">Manuel De Cervantes</author>
           </book>
       </books>
   </value>
</response>

基本上,我试图将我选择的字符串值附加到称为“标签”的所有属性中。这就是“标签”属性是否具有值,以及这些属性是否在xml结构的不同级别。我已经尝试了方法appendNode(),但它又返回了一个时髦的运行时表达式错误。我也查看了这个stackoverflow question,但是我离我所需的答案越来越近了。这是代码:
    def rootFolder = new File('.').getCanonicalPath()
    def response = new XmlSlurper().parse(new File(rootFolder + File.separator + 'src' + File.separator + 'metadata.xml'))
    def tags = response.'**'.findAll { it['@tags']!='' }

    tags.each{ t ->
        def tagAttr =  (t.@tags.value.toString())
        println(tagAttr)
    }

有人对我如何实现这一目标有任何想法吗?

最佳答案

您差点儿明白了...这应该做您想要的(假设我握住了棍子的右端):

def xml = '''<response version-api="2.0">
            |    <value>
            |        <books>
            |            <book available="20" id="1" tags="">
            |                <title>Don Xijote</title>
            |                <author id="1" tags="Joel">Manuel De Cervantes</author>
            |            </book>
            |            <book available="14" id="2" tags="Jane">
            |                <title>Catcher in the Rye</title>
            |               <author id="2" tags="">JD Salinger</author>
            |           </book>
            |           <book available="13" id="3" tags="">
            |               <title>Alice in Wonderland</title>
            |               <author id="3">Lewis Carroll</author>
            |           </book>
            |           <book available="5" id="4" tags="Harry">
            |               <title>Don Xijote</title>
            |               <author id="4">Manuel De Cervantes</author>
            |           </book>
            |       </books>
            |   </value>
            |</response>'''.stripMargin()

import groovy.xml.*

def parsed = new XmlParser().parseText(xml)        

parsed.'**'
      .findAll { 'tags' in it.attributes().keySet().toList() }
      .each { it.@tags += (it.@tags ? ' ' : '') + 'extraTag' }

println XmlUtil.serialize(parsed)

关于xml - 使用Groovy附加到xml文件的属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32914254/

相关文章:

grails - 尝试读取文件但找不到路径?

maven - 使用 Grails Maven 插件跳过集成测试

java - 使用 MS Excel 2007 将 excel 文件中的数据转换为 xml

c# - 将具有多个 namespace 的 XML 反序列化为对象

带有关键帧的 Android 矢量可绘制动画

c# - 仅当 xml 数据从 C# 传递到存储过程时,才在存储过程中使用删除表查询

grails - 在测试中声明SpringSecurity编码的密码

groovy - Gradle 中什么时候需要大括号进行变量替换?

gradle - 确定任务是否在外部 build.gradle 文件中定义

python - 使用 Python 和 BeautifulSoup 从 XML 文件创建字典