cancel
Showing results for 
Search instead for 
Did you mean: 

Getting error "content is not allowed in prolog" while parsing XML through groovy scripts

SOLVED
Highlighted
Occasional Contributor

Getting error "content is not allowed in prolog" while parsing XML through groovy scripts

Hi 

Am using groovy script to parse the XML. Am able get all the data nodes but not able to fetch the particular which i want.

Code 

assert context.response

def getData = { data, element ->
new XmlSlurper().parseText(data).'**'.find {it.name() == element}
}

def inXmlStr = getData(context.response, 'Acknowledge').text()
log.info inXmlStr
def receiptId = getData(inXmlStr, 'ReceiptId')
log.info receiptId
context.testCase.setPropertyValue('ID', receiptId.IdValue.toString())

 

Response XML :

 <?xml version="1.0" encoding="utf-8"?>
<AcknowledgeCandidateRegistration majorVersionId="1" minorVersionId="0" xmlns="http://cebtalentcentral.com">
<ApplicationArea>
<CreationDateTime>2019-02-12T15:11:05.027Z</CreationDateTime>
</ApplicationArea>
<Acknowledge>
<OriginalApplicationArea>
<CreationDateTime>2019-02-12T15:11:03.470Z</CreationDateTime>
</OriginalApplicationArea>
<ReceiptId>155785</ReceiptId> // need this particular id
<Status>Completed</Status>
<RejectCode>0</RejectCode>
<RejectReason />
</Acknowledge>
</AcknowledgeCandidateRegistration>

 

Regards,

Oct21

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

@Oct21 ,

 

Here is the new script assertion for the attached xml

 

 

assert context.response

def getData = { data, element ->
	new XmlSlurper().parseText(data).'**'.find {it.name() == element}
}


def receiptId = getData(context.response, 'ReceiptId')
log.info receiptId
context.testCase.setPropertyValue('ID', receiptId.toString())


Regards,
Rao.
7 REPLIES 7
Community Hero

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

@Oct21,

The above script was provided to you as Script assertion for the Request test step, and that is not (does not work) if you use it in Groovy Script test step.

Can you confirm if you are using the above code in Script Assertion?


Regards,
Rao.
Occasional Contributor

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

The script provided earlier was also used as a groovy test step, and the script am using now is also as a groovy test step.

You can check the screenshot i sent.

Community Hero

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

If you read the post again, it clearly says Scipt Assertion (for the same step where you get the response). No additional step. Please correct it and try again.


Regards,
Rao.
Occasional Contributor

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

@nmrao 

I tried to use the script as script assertion to the request where i am getting the response. But i am getting the same error.

Not sure what has changed, it is parsing the values but not able to pick the one which is described. 

And the past script i used as a groovy script and worked perfectly.

Could please check and help me again. Check the screenshot.

Thanks again.

 

Regards,

Oct21

Community Hero

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

@Oct21,

The response in the earlier question has <CDATA> in it and the response attached in this does not have that. Hence you are getting that.



Regards,
Rao.
Community Hero

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy scri

@Oct21 ,

 

Here is the new script assertion for the attached xml

 

 

assert context.response

def getData = { data, element ->
	new XmlSlurper().parseText(data).'**'.find {it.name() == element}
}


def receiptId = getData(context.response, 'ReceiptId')
log.info receiptId
context.testCase.setPropertyValue('ID', receiptId.toString())


Regards,
Rao.
Occasional Contributor

Re: Getting error "content is not allowed in prolog" while parsing XML through groovy script

Many thanks for the explanation and script @nmrao 

I am now able to understand the XML structure. 

New Here?
Join us and watch the welcome video:
Top Kudoed Authors