groovyxmlslurper

XmlSlurper doesn't return value


i'm using XmlSlurper to get a value from an XML-Response. I copied the response to local String here for easier testing purposes. I need to get the value from jobReqId which is 506 in this case. If I use println: jobReqId.name(); i'm getting the correct node-name (jobReqId). If i'm using println jobReqId or println jobReqId.text(); i'm just getting empty String. Someone has an idea?

def body = """<JobApplicationAssessmentOrder>
        <JobApplicationAssessmentOrder>
            <jobApplication>
                <JobApplication>
                    <applicationTemplateId>562</applicationTemplateId>
                    <lastModifiedDateTime>2020-05-28T11:43:47.000</lastModifiedDateTime>
                    <rating>-1</rating>
                    <source>1</source>
                    <agencyInfo />
                    <reference />
                    <usersSysId />
                    <cust_instr2 />
                    <cust_instr6 />
                    <cust_instr7 />
                    <timeToHire />
                    <currentEmployer />
                    <formerEmployee>false</formerEmployee>
                    <homePhone />
                    <phoneScreenDate />
                    <candComment />
                    <ownershpDate />
                    <firstName>Alex</firstName>
                    <lastModifiedByProxy />
                    <anonymizedDate />
                    <statusComments />
                    <applicationId>382</applicationId>
                    <candidateId>324</candidateId>
                    <dataSource>User Entered</dataSource>
                    <status>Open</status>
                    <lastName>K</lastName>
                    <candTypeWhenHired />
                    <hiredOn />
                    <phoneScreenDetails />
                    <sourceLabel>Corporate Site</sourceLabel>
                    <disabilityStatus />
                    <profileUpdated>0</profileUpdated>
                    <duplicateProfile>0</duplicateProfile>
                    <cust_SourceName />
                    <countryCode>DE</countryCode>
                    <averageRating>-1</averageRating>
                    <owner />
                    <jobReqId>506</jobReqId>
                    <contactEmail>alex@gmx.de</contactEmail>
                    <jobAppGuid />
                    <lastModifiedBy>USER</lastModifiedBy>
                    <nonApplicantStatus>Applied</nonApplicantStatus>
                    <resumeUploadDate />
                    <appStatusSetItemId>762</appStatusSetItemId>
                    <exportedOn />
                    <candConversionProcessed />
                    <referenceComments />
                    <anonymizedFlag>0</anonymizedFlag>
                    <referredBy />
                    <middleName />
                    <appLocale>de_DE</appLocale>
                    <cellPhone>2314</cellPhone>
                    <snapShotDate>2020-05-28T11:42:21.000</snapShotDate>
                </JobApplication>
            </jobApplication>
        </JobApplicationAssessmentOrder>
    </JobApplicationAssessmentOrder>"""

           def xmlBody = new XmlSlurper().parseText(body);
           def jobReqId = xmlBody.JobApplicationAssessmentOrder.JobApplicationAssessmentOrder.jobApplication.JobApplication.jobReqId;
           println jobReqId

Solution

  • Your xmlBody is in the same position as the first JobApplicationAssessmentOrder node. If you do this it works:

     def jobReqId = xmlBody.JobApplicationAssessmentOrder.jobApplication.JobApplication.jobReqId
    

    This is a common mistake, to prevent it, I always do it this way:

    def JobApplicationAssessmentOrder = new XmlSlurper().parseText(body)
    def jobReqId = JobApplicationAssessmentOrder.JobApplicationAssessmentOrder.jobApplication.JobApplication.jobReqId