PHP XML无法获取值

时间:2015-11-30 08:46:57

标签: php xml xml-parsing

我有一个这样的字符串:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems>
<rs:data>
   <z:row ows_MetaInfo='128;#' />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>

我想得到值128。

我已经尝试过simplexml_load_string,它是空的。

我如何获得此属性?

1 个答案:

答案 0 :(得分:0)

命名空间在soap响应中很重要。请尝试以下代码:

<?php
$xml = '<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
            <GetListItemsResult>
                <listitems>
                    <rs:data>
                        <z:row ows_MetaInfo=\'128;#\'/>
                    </rs:data>
                </listitems>
            </GetListItemsResult>
        </GetListItemsResponse>
    </soap:Body>
</soap:Envelope>';

$xml_element = simplexml_load_string($xml);
$name_spaces = $xml_element->getNamespaces(true);
$soap = $xml_element->children($name_spaces['soap'])
    ->Body
    ->children($name_spaces['rs'])
    ->GetListItemsResponse
    ->GetListItemsResult
    ->listitems
    ->{'rs:data'}
    ->{'z:row'}['ows_MetaInfo'][0];

echo (string) $soap;
?>