本文介绍了PHP XML 无法获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:

I have a string like this:

<?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,它是空的.

I already tried simplexml_load_string which is empty.

我怎样才能获得这个属性?

How can I get this attribute?

推荐答案

命名空间在soap响应中很重要.试试下面的代码:

Namespace is important in soap response. Try below code:

<?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;
?>

这篇关于PHP XML 无法获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 07:26