I have a requirement where I am collecting system and some product specific information, so is there a way I can append the already existing xml’s content to the product tag, in my xml builder?
My test code to create a xml using groovy
import groovy.xml.MarkupBuilder
import org.custommonkey.xmlunit.*
def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder(writer)
def xml=new XmlSlurper().parse("E:\\DomainMeta.xml")
builder.csm() {
system(osname:"linux", hostname:"panther")
product()
{
//Here i'd like to add my xml content, starting with <DomainMeta>
}
}
println writer.toString()
My xml, which has to be copied into another xml which i am building above
<DomainMeta> //there can be more dynamic information inside DomainMeta tag
<Gateways>
<NodeRef name="N_116489" host="panther" httpPort="18,448" port="18,449" />
</Gateways>
<OptionGroup name="DomainOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
<OptionGroup name="NodeOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
</DomainMeta>
Desired output
<csm>
<system osname='linux' hostname='panther' />
<product>
<DomainMeta>
<Gateways>
<NodeRef name="N_116489" host="panther" httpPort="18,448" port="18,449" />
</Gateways>
<OptionGroup name="DomainOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
<OptionGroup name="NodeOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
</DomainMeta>
</product>
</csm>
Update
One more Problem, The question that i asked is answered by tim_yates but i am facing another problem here, the xml data which comes as a dump is in a format given below, @tim: can you help?
<DomainMeta>
<Gateways>
<NodeRef name="N_116489" host="panther" httpPort="18,448" port="18,449" />
</Gateways>
<OptionGroup name="DomainOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
<OptionGroup name="NodeOptions">
<Option name="LicenseUsageDetailMinDays" value="90" ></Option>
</OptionGroup>
</DomainMeta>
You can do it if you use StreamingMarkupBuilder like so:
Edit — Answer to secondary unrelated question
If you have that in a variable called
txt, you should be able to do:Then, pass the
txttoXmlSlurper.parseText()Though really, I’d say that this is an issue with the thing that is writing the source out so it is unusable as xml, but pretty on the web.