Hi,
As part of integrating Webex conferencing service to my current working project, I came across on parsing XML which contains tags with colon (:). I was familiar with normal XML parsing [means XML tags without colon] and I have done a lots of such XML parsing. After searching on google for sometime, I found solution for parsing XML with tags which contains colon. I would like to share this solution with an example here. Please see below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | //A sample XML response from Webex conference service $xmlResponse = '<?xml version="1.0" encoding="ISO-8859-1"?> <serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" xmlns:com="http://www.webex.com/schemas/2002/06/common" xmlns:sess="http://www.webex.com/schemas/2002/06/service/session" xmlns:train="http://www.webex.com/schemas/2002/06/service/trainingsession" xmlns:qti="http://www.webex.com/schemas/2002/06/service/trainingsessionqti" xmlns:qtiasi= "http://www.webex.com/schemas/2002/06/service/trainingsessionqtiasi"> <serv:header> <serv:response> <serv:result>SUCCESS</serv:result> <serv:gsbStatus>PRIMARY</serv:gsbStatus> </serv:response> </serv:header> <serv:body> <serv:bodyContent xsi:type="train:createTrainingSessionResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <train:sessionkey>807643542</train:sessionkey> <train:additionalInfo> <train:sessionkey>807643542</train:sessionkey> <train:guestToken>514d8fd53f1ce7d22107bbc25e586fc1 </train:guestToken> </train:additionalInfo> </serv:bodyContent> </serv:body> </serv:message>'; //Simply convert the above XML string XML object $xmlObj = new SimpleXMLElement($xmlResponse); //Getting result status $resultStatus = $xmlObj->children('serv', true)->header->children('serv', true)->response->children('serv', true)->result; //Getting session key associated $strSessionKey = $xmlObj->children('serv', true)->body->children('serv', true)->bodyContent->children('train', true)->sessionkey; //Getting guest token too $strSessionGuestToken = $xmlObj->children('serv', true)->body->children('serv', true)->bodyContent->children('train', true)->additionalInfo->children('train', true)->guestToken; echo 'Result Status : ' . $resultStatus . '<br/>'); echo 'Session Key : ' . $strSessionKey . '<br/>'); echo 'Guest Token : ' . $strSessionGuestToken . '<br/>'); |