Given that your response looks something like this:
...
<phoneNumber>
<countryDialInNumber>1</countryDialInNumber>
<mobileFlag>true</mobileFlag>
<phoneNumber>5128797895</phoneNumber>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
...
...
and that you want the value for the phone number, in the above example 5128797895, than you can use a variation of jhunter's solution, here you go:
def xml = '''<xml>
<phoneNumber>
<countryDialInNumber>1</countryDialInNumber>
<mobileFlag>true</mobileFlag>
<phoneNumber>5128797895</phoneNumber>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>PRM</type>
<notificationPreference>
<method>SMS</method>
<optIn>false</optIn>
<preferredLanguage>en-US</preferredLanguage>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>TXNN</type>
</notificationPreference>
<notificationPreference>
<method>SMS</method>
<optIn>true</optIn>
<preferredLanguage>en-US</preferredLanguage>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>MKTN</type>
</notificationPreference>
</phoneNumber>
<phoneNumber>
<countryDialInNumber>1</countryDialInNumber>
<mobileFlag>true</mobileFlag>
<phoneNumber>6124045907</phoneNumber>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>SEC</type>
<notificationPreference>
<method>SMS</method>
<optIn>false</optIn>
<preferredLanguage>en-US</preferredLanguage>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>MKTN</type>
</notificationPreference>
<notificationPreference>
<method>SMS</method>
<optIn>false</optIn>
<preferredLanguage>en-US</preferredLanguage>
<startDate>2018-10-19T21:10:52-05:00</startDate>
<stronglyAuthenticated>false</stronglyAuthenticated>
<type>TXNN</type>
</notificationPreference>
</phoneNumber>
</xml>
'''
new XmlSlurper().parseText(xml).'**'.findAll {
it.name() == 'phoneNumber' && !it.childNodes()
}.each {
log.info it //this will output the phone numbers only!
}
The result will look like this:
Mon Oct 29 18:55:19 CET 2018:INFO:5128797895
Mon Oct 29 18:55:19 CET 2018:INFO:6124045907
To write the results into a file, edit the below code to your desired destination and format:
def file = new File("<your filepath and filename here, e.g. c:/output.txt>")
new XmlSlurper().parseText(xml).'**'.findAll {
it.name() == 'phoneNumber' && !it.childNodes()
}.each {
log.info it //this will output the phone numbers only!
file << it // see also here: http://mrhaki.blogspot.com/2009/08/groovy-goodness-working-with-files.html
}