简体   繁体   中英

How to add a node to groovy MarkupBuilder?

I am using MarkupBuilder to generate xml, need to know how can I add NodeChild to a MarkupBuilder Object

my code

def fxml=new File("E:\\Projects\\dom.xml")
def xmltext=new XmlSlurper(false,false).parseText(fxml.text)

    or

    def xml=new XmlSlurper(false,false).parse("E:\\Projects\\dom.xml")

def abc = new groovy.xml.MarkupBuilder()
abc.product(name:"Dota"){
  language("Java")
  language("Groovy")
  language("JavaScript")
  domainsinfa{delegate.current.appendNode( xmltext)}
}

You can use StreamingMarkupBuilder to insert arbitrary nodes to the xml:

import groovy.xml.*

def xmltext = '''<node><name short="yes">tim</name><fun>maybe</fun></node>'''

def xml = new XmlSlurper( false, false ).parseText( xmltext )

def newxml = new StreamingMarkupBuilder().bind {
  product(name:"Dota") {
    language("Java")
    language("Groovy")
    language("JavaScript")
    mkp.yield xml 
  }
}

println XmlUtil.serialize( newxml )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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