XmlSlurper namespaces not being used
I have an XML template document:
<TriadMessage xmlns="http://www.myco.com/02/11/2008/V1/TriadMessage.xsd"
xmlns:triad="http://www.myco.com/02/11/2008/V1/TriadTypes.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<TriadRouteInfo>
<triad:RoutingCorrelationId>new value goes here</triad:RoutingCorrelationId>
...
that I read using Groovy 1.7:
triadDoc = new XmlSlurper().parseText(xmlMessage).declareNamespace(
tm: "http://www.myco.com/02/11/2008/V1/TriadMessage.xsd",
triad: "http://www.myco.com/02/11/2008/V1/TriadTypes.xsd",
xsi: "http://www.w3.org/2001/XMLSchema-instance"
)
xmlBuilder = new StreamingMarkupBuilder()
writer = xmlBuilder.bind {mkp.yield triadDoc}
I then insert values into the document:
triadDoc.TriadRouteInfo.RoutingCorrelationId = dto.getReportRevisionId()
...
and output:
writer.toString()
This is what my document looks like:
<?xml version="1.0" encoding="UTF-8"?>
<tm:TriadMessage xmlns:tm="http://www.myco.com/02/11/2008/V1/TriadMessage.xsd">
<tm:TriadRouteInfo>
<triad:RoutingCorrelationId xmlns:triad="http://www.myco.com/02/11/2008/V1/TriadTypes.xsd">24670</triad:RoutingCorrelationId>
...
Notice that the triad namespace appears with each element. There are a lot of these lines (I only show one here). I want to have th开发者_如何转开发e NS declared at the top only and referred via triad: below. What am I doing wrong?
Just tried this:
import groovy.xml.StreamingMarkupBuilder
def xml = """
<TriadMessage xmlns="http://www.myco.com/02/11/2008/V1/TriadMessage.xsd"
xmlns:triad="http://www.myco.com/02/11/2008/V1/TriadTypes.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<TriadRouteInfo>
<triad:RoutingCorrelationId>new value goes here</triad:RoutingCorrelationId>
</TriadRouteInfo>
</TriadMessage>"""
def triadDoc = new XmlSlurper().parseText( xml ).declareNamespace(
tm: "http://www.myco.com/02/11/2008/V1/TriadMessage.xsd",
triad: "http://www.myco.com/02/11/2008/V1/TriadTypes.xsd",
xsi: "http://www.w3.org/2001/XMLSchema-instance" )
def xmlBuilder = new StreamingMarkupBuilder()
writer = xmlBuilder.bind {
mkp.declareNamespace( tm: "http://www.myco.com/02/11/2008/V1/TriadMessage.xsd" )
mkp.declareNamespace( triad: "http://www.myco.com/02/11/2008/V1/TriadTypes.xsd" )
mkp.declareNamespace( xsi: "http://www.w3.org/2001/XMLSchema-instance" )
mkp.yield triadDoc
}
triadDoc.TriadRouteInfo.RoutingCorrelationId = 'wheeee'
println writer
And it printed out this (prettied up by me, so it's not all on one line):
<tm:TriadMessage xmlns:tm='http://www.myco.com/02/11/2008/V1/TriadMessage.xsd'
xmlns:triad='http://www.myco.com/02/11/2008/V1/TriadTypes.xsd'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<tm:TriadRouteInfo>
<triad:RoutingCorrelationId>wheeee</triad:RoutingCorrelationId>
</tm:TriadRouteInfo>
</tm:TriadMessage>
Any good?
精彩评论