Add to existing groovy.util.Node with groovy.xml.MarkupBuilder syntax?

I am working with an API that returns groovy.util.Node, allowing me to customize its generated XML. I need to add a child to Node, and I am wondering if I can use MarkupBuilder syntax to change Node.

For example, something works here, but it seems klunky:

withXml { rootNode ->
    def appendedNode = new Node(rootNode, 'foo', [name:'bar'])
    def appendedNodeChild = new Node(appendedNode, 'child', [blah:'baz'])
}

Is there a way to add MarkupBuilder-ish syntax to rootNode? Thank.

+5
source share
3 answers

You can use the appendNode groovy.util.Node method:

withXml {rootNode ->
    rootNode.appendNode ('foo', [name: 'bar']). appendNode ('child', [blah: 'baz'])
}

add

<foo name="bar">
    <child blah="baz"/>
</foo>

rootNode.

+3
new MarkupBuilder().root {
   foo( name:'bar' ) {
     child( blah:'blaz' )
   }
 }

, , , - ,

,

http://groovy.codehaus.org/api/groovy/xml/MarkupBuilder.html

0

groovy.util.Node javadoc : "Closure" :

  • void plus(Closure c)
  • Node replaceNode(Closure c)

, - . spring build.gradle:

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java

            pom.withXml {
                ((groovy.util.Node) asNode()).children().first() + {
                    setResolveStrategy(Closure.DELEGATE_FIRST)
                    parent {
                        groupId 'org.springframework.boot'
                        artifactId 'spring-boot-starter-parent'
                        version "${springBootVersion}"
                    }
                    description 'A demonstration of maven POM customization'
                }
            }
        }
    }
}
0

All Articles