Mindaugas B
Mindaugas B

Reputation: 1

Groovy xml request parsing

I am new to groovy.

Trying to parse some xml request, without luck for some time.

As Final result:

  1. I want to check if xml request "RequestRecords" has "DetailsRequest" atrribute;
  2. Get "FieldValue" number where "RequestF" has FieldName="Id".

Also, for some reason I cannot use XmlSlurper as it returns false to 'def root = new XmlParser().parseText(xml)'.

def env = new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/", 'env');
def ns0 = new groovy.xml.Namespace("http://tempuri.org/", 'ns0')

def xml = '''<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns0="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Body>
    <ns0:Request1>
        <ns0:Request_Sub>
            <ns0:RequestRecords TableName="Header">
                <ns0:RequestF FieldName="SNumber" FieldValue="XXX"/>
                <ns0:RequestF FieldName="TNumber" FieldValue="30"/>
            </ns0:RequestRecords>
            <ns0:RequestRecords TableName="Details">
                <ns0:RequestF FieldName="Id" FieldValue="1487836040"/>
            </ns0:RequestRecords>
        </ns0:Request_Sub>
        <ns0:isOffline>false</ns0:isOffline>
    </ns0:Request1>
</env:Body>
</env:Envelope>'''

def root = new XmlParser().parseText(xml)

println ("root" + root)

assert "root_node" == root.name()
println root_node    

Even assertion of root node fails.

Upvotes: 0

Views: 470

Answers (2)

tim_yates
tim_yates

Reputation: 171084

Given that XML, you can use XmlSlurper to get the answers to your two questions like so:

def root = new XmlSlurper().parseText(xml)

// I want to check if xml request "RequestRecords" has "DetailsRequest" atrribute
List<Boolean> hasAttribute = root.Body
                                 .Request1
                                 .Request_Sub
                                 .RequestRecords
                                 .collect { it.attributes().containsKey('DetailsRequest') }
assert hasAttribute == [false, false]

// Get "FieldValue" number where "RequestF" has FieldName="Id".
String value = root.Body
                   .Request1
                   .Request_Sub
                   .RequestRecords
                   .RequestF
                   .find { it.@FieldName == 'Id' }?.@FieldValue

assert value == '1487836040'

Upvotes: 1

David M. Karr
David M. Karr

Reputation: 15225

Either XmlSlurper or XmlParser should work fine. From what I can see, they appear to be functionally equivalent. They only differ in memory usage and performance.

It looks like you copied this code from the example in the javadoc, without realizing what some of the pieces mean. Clearly, the assertion for the "root node" fails because the name of the root node from the example was "root_node", but it's "Envelope" from your code.

Not sure why you're saying that XmlSlurper doesn't work. Your code sample using it doesn't even use it.

Upvotes: 0

Related Questions