[prev in list] [next in list] [prev in thread] [next in thread] 

List:       groovy-user
Subject:    Re: [groovy-user] XmlSlurper: I can find a node but toString is always empty...?
From:       Pascal Schumacher <pascalschumacher () gmx ! net>
Date:       2013-09-27 21:13:34
Message-ID: 5245F4FE.60005 () gmx ! net
[Download RAW message or body]

Hi,

how do you access the node?

I guess you use something like:
new XmlSlurper().parseText(xml).Body.NodeA.NodeB.NodeBChild1

This does not work, because the GPathResult returned by XmlSlurper has a 
method getBody():
http://groovy.codehaus.org/api/groovy/util/slurpersupport/GPathResult.html#getBody%28%29

Body is just a shortcut for this method. You have to quote Body e.g.:

new XmlSlurper().parseText(xml).'Body'.NodeA.NodeB.NodeBChild1

This works for me (with Groovy 2.1.7):

def xml = '''<SOAP-ENV:Envelope 
xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/' 
xmlns:ns1='http://x.com/something'>
<SOAP-ENV:Body>
<NodeA xmlns='http://x.com/something'>
<NodeB>
    <NodeBChild1>ValueX</NodeBChild1>
    <NodeBChild2>SomeValue</NodeBChild2>
</NodeB>
</NodeA>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''

assert new XmlSlurper().parseText(xml).'Body'.NodeA.NodeB.NodeBChild1 == 
'ValueX'

I guess almost everybody who uses XmlSlurper with SOAP stumbles over 
this problem. At least I did and some of my colleges too.

- Pascal


Am 27.09.2013 21:22, schrieb Amy B:
> Hi,
>
> I've been able in the past to modify contents of slurped XML just by
> assigning a new value like this:
>
> xml.nodename = '20090101'
>
> I have XML for which that is not working for me right now.
>
> I have the slurper defined as follows. I am retrieving the XML from a
> database table.
>
>
>          def baseRequest = new
> XmlSlurper().parseText(variableContainingRetrievedXML)
>
>
> Structure is something like this.
>
> <SOAP-ENV:Envelope
> xmlns:SOAP-ENV='http://schemas.xmlsoap.org/soap/envelope/'
> xmlns:ns1='http://x.com/something'>
> <SOAP-ENV:Body>
> <NodeA xmlns='http://x.com/something'>
> <NodeB>
>     <NodeBChild1>ValueX<</NodeBChild1>
>     <NodeBChild2>SomeValue</NodeBChild2>
> </NodeB>
> </NodeA>
> </SOAP-ENV:Body>
> </SOAP-ENV:Envelope>
>
> I can FIND NodeBChild1 but its toString() value always comes back empty.
>
> Nothing I've tried has worked.
>
> Thoughts?
>
> Thanks.
>


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic