+ Start a Discussion

Regex to strip XML



I have a callout which returns a soap xml message. From within this message I need to extract the following value:


<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><RegisterUserWithAutoIdResponse xmlns="http://website/PartnerRegistrationWebservice/registration"><RegisterUserWithAutoIdResult><registrationResponse clientid="4031972" success="True" xmlns=""><clientName value="Test" success="True" errorCode="0" /><password success="True" errorCode="0" /><loginName value="4031972" success="True" errorCode="0" /></registrationResponse></RegisterUserWithAutoIdResult></RegisterUserWithAutoIdResponse></soap:Body></soap:Envelope>


I used the following lines in my Apex script to somehow get this data:


string xml = 'the above string'

string XML_STRIP = '(?<=clientid=")([0-9]*+)(?=")';
pattern myPattern = pattern.compile(XML_STRIP);
matcher myMatcher = myPattern.matcher(xml);
xml = myMatcher.replaceAll('');

System.debug('TEST: ' + xml);


This however, returns the whole string without the value (clientid). It needs to be the other way around.


I tried doing this with XmlStreamReader but that didn't work as the values are inside the tags I think.


Any help would be appreciated.