douhongxie5436 2012-04-25 19:30 采纳率: 100%
浏览 71
已采纳

使用SimpleXML和xpath解析Zimbra SOAP响应

So, I'm using PHP to talk to a Zimbra SOAP server. The response is in a <soap:Envelope> tag. I'm having trouble parsing the XML response because of the namespace(s).

The XML looks like this:

<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="iamtesting@example.com">
        <a n="zimbraId">83ebf344-dc51-47ae-9a36-3eb24281d53e</a>
        <a n="zimbraMailDeliveryAddress">iamtesting@example.com</a>
      </account>
    </CreateAccountResponse>
  </soap:Body>
</soap:Envelope>

I make a new SimpleXMLElement object:

$xml = new SimpleXMLElement($data);

After Googling a bit, I found I need to register the namespace. So I do that:

$xml->registerXPathNamespace('soap', 'http://www.w3.org/2003/05/soap-envelope');

Then I can get the <soap:Body> tag easily.

$body = $xml->xpath('//soap:Body');

But I can't get any elements after that (using xpath):

$CreateAccountResponse = $xml->xpath('//soap:Body/CreateAccountResponse');

This returns an empty array. I can traverse the XML though, to get that element.

$CreateAccountResponse = $body[0]->CreateAccountResponse;

This works fine, but now I want to get the <a> tags, specifically the zimbraId one. So I tried this:

$zimbraId = $CreateAccountResponse->account->xpath('a[@n=zimbraId]');

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 soap:)?

How can I get the <a> tags based on their n attribute?

P.S. I'm aware that the id and name are also in the <account> tag's attributes, but there are a bunch more <a> tags that I want to get using the n attribute.

Note: I'm trying to improve the Zimbra library for my application for work. The current code to get the <a> tags is as follows:

$zimbraId = strstr($data, "<a n=\"zimbraId\"");
$zimbraId = strstr($zimbraId, ">");
$zimbraId = substr($zimbraId, 1, strpos($zimbraId, "<") - 1);

Obviously, I want to remove this code (there's also some regexes (shudder) later on in the code), and use an XML parser.

  • 写回答

1条回答 默认 最新

  • douchan6512 2012-04-25 20:49
    关注

    The elements you want to retrieve have a namespace as well, namely urn:zimbraAdmin.

        <CreateAccountResponse xmlns="urn:zimbraAdmin">
    

    The xmlns attribute states the default namespace for any child elements, so the elements you are trying to retrieve actually have a namespace, even though no prefix is used (see the wikipedia article for some examples). If you specify a namespace prefix as you did for http://www.w3.org/2003/05/soap-envelope you should be fine.

    $xml->registerXPathNamespace('soap', 'http://www.w3.org/2003/05/soap-envelope');
    $xml->registerXPathNamespace('zimbra', 'urn:zimbraAdmin');
    $CreateAccountResponse = $xml->xpath('//soap:Body/zimbra:CreateAccountResponse');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型