Forum Discussion

mamar's avatar
mamar
Contributor
12 years ago

[Resolved] How to fetch specific element values

Hi,

I have a below response from my test. Here i need to find and fetch all the amount values from the element <CompositeCurAmtType> having value as "TranTotal" only. Can anyone help me on how to get though groovy. (From the below response, i need only $10.00 and $77.00).

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<com.fnf:LoanAcctTrnRec xmlns:com.fnf="http://www.fnf.com/xes">
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>TranTotal</CompositeCurAmtType>
<CurAmt>
<Amt>10.00</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>CalcRunningPrincipalBal</CompositeCurAmtType>
<CurAmt>
<Amt>20.00</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>Fees/Charges</CompositeCurAmtType>
<CurAmt>
<Amt>30.25</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>TranTotal</CompositeCurAmtType>
<CurAmt>
<Amt>77.00</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>CalcRunningPrincipalBal</CompositeCurAmtType>
<CurAmt>
<Amt>46.00</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
<CompositeCurAmt xmlns="http://www.ifxforum.org/IFX_150">
<CompositeCurAmtType>Fees/Charges</CompositeCurAmtType>
<CurAmt>
<Amt>85.25</Amt>
<CurCode>USD</CurCode>
</CurAmt>
</CompositeCurAmt>
</com.fnf:LoanAcctTrnRec>
</soapenv:Body>
</soapenv:Envelope>
  • Hello,

    //Script to find Amt and Currency Code for CompositeCurAmtType = "TranTotal"

    groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    holder = groovyUtils.getXmlHolder( "TESTCASE_NAME#Response" )
    holder.namespaces["ns"] = "http://www.ifxforum.org/IFX_150"


    // Get Count of CompositeCurAmt nodes
    countstr = holder["count(//ns:CompositeCurAmt"]
    int count = countstr.toInteger()
    log.info count

    for(i = 1; i <=count ; i++)
    {
    Type = holder.getNodeValues("//ns:CompositeCurAmt/ns:CompositeCurAmtType["+ i +"]")
    Type = Type.toString()
    if(Type.equals("TranTotal"))
    {
    Amt = holder.getNodeValues("//ns:CompositeCurAmt/ns:CompositeCurAmtType["+ i +"]/ns:CurAmt/ns:Amt" )
    CurCode = holder.getNodeValues("//ns:CompositeCurAmt/ns:CompositeCurAmtType["+ i +"]/ns:CurAmt/ns:CurCode" )
    log.info "Amount" + Amt
    log.info "Currency Code" + CurCode
    }
    }

    Hope it Helps...
  • Thanks for your time and code.
    I used the code and dint get the required values, instead i get null as output. I changed a little and able to get what i need. Find the below code for your reference, if you need.

    groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    holder = groovyUtils.getXmlHolder( "doLoanAcctTrnInq#Response" )
    holder.namespaces["fnf"] = "http://www.fnf.com/xes"
    holder.namespaces["ns"] = "http://www.ifxforum.org/IFX_150"
    countLoanAcctTrnRec = holder.getNodeValue("count(//fnf:LoanAcctTrnRec)")
    countLoanAcctTrnRec = countLoanAcctTrnRec.toInteger()
    log.info "countLoanAcctTrnRec : $countLoanAcctTrnRec"
    String CurAmtType

    for(i = 1; i<=countLoanAcctTrnRec ; i++)
    {
    countCompositeCurAmt = holder.getNodeValue("count(//fnf:LoanAcctTrnRec["+ i +"]//ns:CompositeCurAmt)")
    countCompositeCurAmt = countCompositeCurAmt.toInteger()
    log.info "countCompositeCurAmt : $countCompositeCurAmt"
    for(j = 1; j<=countCompositeCurAmt; j++)
    {
    CurAmtType = holder.getNodeValue("//fnf:LoanAcctTrnRec["+ i +"]//ns:CompositeCurAmt["+ j +"]//ns:CompositeCurAmtType")
    log.info "CurAmtType : $CurAmtType"
    if(CurAmtType.equals("TranTotal"))
    {
    CurAmt = holder.getNodeValue("//fnf:LoanAcctTrnRec["+ i +"]//ns:CompositeCurAmt["+ j +"]//ns:CurAmt//ns:Amt")
    CurCode = holder.getNodeValue("//fnf:LoanAcctTrnRec["+ i +"]//ns:CompositeCurAmt["+ j +"]//ns:CurAmt//ns:CurCode")
    log.info "CurAmt : $CurAmt"
    log.info "CurCode : $CurCode"
    }
    }
    }