Parsing Zimbra SOAP response with SimpleXML and xpath

Ksyxrjjmldbrm

New Member
So, I'm using PHP to talk to a Zimbra SOAP server. The response is in a \[code\]<soap:Envelope>\[/code\] tag. I'm having trouble parsing the XML response because of the namespace(s).The XML looks like this:\[code\]<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"> <soap:Header> <context xmlns="urn:zimbra"> <change token="20333"/> </context> </soap:Header> <soap:Body> <CreateAccountResponse xmlns="urn:zimbraAdmin"> <account id="83ebf344-dc51-47ae-9a36-3eb24281d53e" name="[email protected]"> <a n="zimbraId">83ebf344-dc51-47ae-9a36-3eb24281d53e</a> <a n="zimbraMailDeliveryAddress">[email protected]</a> </account> </CreateAccountResponse> </soap:Body></soap:Envelope>\[/code\]I make a new \[code\]SimpleXMLElement\[/code\] object:\[code\]$xml = new SimpleXMLElement($data);\[/code\]After Googling a bit, I found I need to register the namespace. So I do that:\[code\]$xml->registerXPathNamespace('soap', 'http://www.w3.org/2003/05/soap-envelope');\[/code\]Then I can get the \[code\]<soap:Body>\[/code\] tag easily.\[code\]$body = $xml->xpath('//soap:Body');\[/code\]But I can't get any elements after that (using xpath):\[code\]$CreateAccountResponse = $xml->xpath('//soap:Body/CreateAccountResponse');\[/code\]This returns an empty array. I can traverse the XML though, to get that element.\[code\]$CreateAccountResponse = $body[0]->CreateAccountResponse;\[/code\]This works fine, but now I want to get the \[code\]<a>\[/code\] tags, specifically the \[code\]zimbraId\[/code\] one. So I tried this:\[code\]$zimbraId = $CreateAccountResponse->account->xpath('a[@n=zimbraId]');\[/code\]No luck, I get a blank array. What's going on? Why can't I use xpath to get elements (that don't start with \[code\]soap:\[/code\])?How can I get the \[code\]<a>\[/code\] tags based on their \[code\]n\[/code\] attribute?P.S. I'm aware that the \[code\]id\[/code\] and \[code\]name\[/code\] are also in the \[code\]<account>\[/code\] tag's attributes, but there are a bunch more \[code\]<a>\[/code\] tags that I want to get using the \[code\]n\[/code\] attribute.Note: I'm trying to improve the Zimbra library for my application for work. The current code to get the \[code\]<a>\[/code\] tags is as follows:\[code\]$zimbraId = strstr($data, "<a n=\"zimbraId\"");$zimbraId = strstr($zimbraId, ">");$zimbraId = substr($zimbraId, 1, strpos($zimbraId, "<") - 1);\[/code\]Obviously, I want to remove this code (there's also some regexes (shudder) later on in the code), and use an XML parser.
 
Back
Top