Reputation: 12347
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>
Upvotes: 2
Views: 1839
Reputation: 12347
This is what i did
The problem, during xml markup builder if we give a string containing xml/html content it converts <
& >
to <
& >
respectively so in order to avoid this replacing all <
& >
with <
& >
might work, but sometimes you have CDATA section which might again contain <, which i don't want to replace, so i used mkp.yieldUnescaped content
File domainMetaFile = new File(System.getProperty("user.dir")+"/DomainMeta.xml")
if ( domainMetaFile.exists( ) )
{
String content = domainMetaFile.getText()
def rootNode= domainMetaFile.readLines().get(0)
//getting the root node
def node=rootNode.find("[\\w\\d]+")
content=content.replaceAll("</"+node+">","").replaceAll("<"+node+">","")
//removing the root node from the string $content
csm."$node"{mkp.yieldUnescaped content}
//adding $content to my existing xml builder (csm)
//Here i am using mkp.yieldUnescaped content to ignore escaping of HTML/XML characters
}
else
csm.DomainMeta(na){}
Upvotes: 0
Reputation: 171084
You can do it if you use StreamingMarkupBuilder like so:
import groovy.xml.XmlUtil
import groovy.xml.StreamingMarkupBuilder
def xml = new XmlSlurper().parse("E:\\DomainMeta.xml")
def out = new StreamingMarkupBuilder().bind {
csl {
system(osname:"linux", hostname:"panther")
product() {
mkp.yield xml
}
}
}
println XmlUtil.serialize( out )
If you have that in a variable called txt
, you should be able to do:
txt = txt.replace( '<', '<' )
.replace( '>', '>' )
Then, pass the txt
to XmlSlurper.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.
Upvotes: 5