Search code examples
xmlgroovy

How to get the value of a specific tag inside XML


I want to get specific value from message wsdl xml like this:

<Message name="InvestorAcctStatement">
     <Field name="ExtRef">180120120000331</Field>
     <Field name="SeqNum">000001</Field>
     <Field name="AC">0000000800100007182</Field>
     <Field name="CurCod">IDR </Field>
     <Field name="ValDate">20090703</Field>
     <Field name="OpenBal">87928063.0000</Field>
     <List name="StatLine">
        <Record name="StatLine">
                    <Field name="ExtRef">180120120000331</Field>
                     <Field name="TrxType">N9293</Field>
                     <Field name="DC">C</Field>
                    <Field name="CashVal">10000.0000</Field>
                    <Field name="Description1">0800100007182</Field>
                    <Field name="Description2">959508221001000</Field>
                    <Field name="Description3">324242</Field>
        </Record>
    </List>
    <Field name="CloseBal">87938063.0000</Field>
     <Field name="Notes">DR  0640100002095 KE 0800100007182      </Field>
</Message>

How can groovy get specific value from that xml?

I already tried with this code:

String dbresponse = '''
<Message name="InvestorAcctStatement">
     <Field name="ExtRef">180120120000331</Field>
     <Field name="SeqNum">000001</Field>
     <Field name="AC">0000000800100007182</Field>
     <Field name="CurCod">IDR </Field>
     <Field name="ValDate">20090703</Field>
     <Field name="OpenBal">87928063.0000</Field>
     <List name="StatLine">
        <Record name="StatLine">
                    <Field name="ExtRef">180120120000331</Field>
                     <Field name="TrxType">N9293</Field>
                     <Field name="DC">C</Field>
                    <Field name="CashVal">10000.0000</Field>
                    <Field name="Description1">DR  0640100002095 KE 0800100007182      </Field>
                    <Field name="Description2">959508221001000                         </Field>
                    <Field name="Description3">                                        </Field>
        </Record>
    </List>
    <Field name="CloseBal">87938063.0000</Field>
     <Field name="Notes">DR  0640100002095 KE 0800100007182      </Field>
</Message>

'''
xml = new XmlParser().parseText(dbresponse)

xml.each { 
    if(it.@name == 'ExtRef') {
        println it
    }
}

When I run it, it returns

Field[attributes={name=ExtRef}; value=[180120120000331]]

I don't know how to call the specific value, especially like field name CashVal on that xml code.


Solution

  • In order to print the values in your code try to modify your loop in this way:

    xml.each {
        if(it.@name == 'ExtRef') {
            println "Name: ${it.@name}"
            println "Value: ${it.text()}"
        }
    }
    

    Regarding CashVal, here is very rough implementation:

    xml.each {
        if (it.@name == 'StatLine') {
            it.'*'.'*'.each {
                if (it.@name == 'CashVal') {
                    def cashVal = new BigDecimal(it.text())
                    println "cashVal = $cashVal"
                }
            }
        }
    }
    

    Just for you to get an idea.