SOAP XML在PHP中读取

时间:2013-11-20 04:37:29

标签: php xml

我无法阅读soap xml file。我需要在数组中使用partition元素

这是我的服务回复:

<?php $xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <GetNetworkTopologyResponse xmlns="http://tempuri.org/">
            <GetNetworkTopologyResult>
                <Action Type="NetworkTopology" Result="OK">
                    <topology MinVSATID="1280" MaxVSATID="376">
                        <partition UID="1" NAME="System">
                            <lvg UID="3605" NAME="BS_TEST"/>
                            <lvg UID="4452" NAME="Colocated VATS BH"/>
                            <lvg UID="4530" NAME="SkyMesh_V40"/>
                            <lvg UID="4845" NAME="Bordernet_V45"/>
                            <lvg UID="4846" NAME="HarbourIT_V42"/>
                            <lvg UID="4847" NAME="AN_V43"/>
                            <lvg UID="4848" NAME="ANT_V44"/>
                            <lvg UID="4850" NAME="Clrnet_V41"/>
                            <lvg UID="4851" NAME="Oion_V46"/>
                            <lvg UID="14072" NAME="NBNCo_V47"/>
                            <lvg UID="15811" NAME="iit_V50"/>
                            <lvg UID="15812" NAME="IPStar_V49"/>
                            <lvg UID="17784" NAME="COLOCATED"/>
                            <lvg UID="20655" NAME="Bluemaxx_V52"/>
                            <lvg UID="21333" NAME="SkyMesh_V55"/>
                            <lvg UID="22782" NAME="Indigo_V54"/>
                            <lvg UID="24836" NAME="NBDemo_V51"/>
                            <wg UID="457" NAME="OB_1" LAPUADDRESS="256" HSP="65" HSPIP="2.17.1.1"/>
                            <wg UID="25397" NAME="OB_1_VoIP" LAPUADDRESS="260"/>
                            <wg UID="27795" NAME="OB_1_WE" LAPUADDRESS="258"/>
                            <sg UID="8881" NAME="OB_60_52_00_14" LAPUADDRESS="515" VERSION="6.5.00.14">
                                <dataTPL UID="25197" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="25195" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="25193" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="25191" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="17031" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="14168" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="8876" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="8874" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                            </sg>
                            <sg UID="25396" NAME="OB_60_52_00_22" LAPUADDRESS="517" VERSION="6.2.00.22">
                                <dataTPL UID="27598" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27596" NAME="DPS_12_6_52_00_22_VLAN_61-70"/>
                                <dataTPL UID="27594" NAME="DPS_11_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27592" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27590" NAME="DPS_12_6_52_00_22_VLAN_37-52"/>
                                <dataTPL UID="27588" NAME="DPS_12_6_52_00_22_VLAN_53-60"/>
                                <dataTPL UID="27586" NAME="DPS_11_6_52_00_22_VLAN_53-60"/>
                                <dataTPL UID="25395" NAME="DPS_11_6_52_00_22_VLAN_37-52"/>
                            </sg>
                            <sg UID="27796" NAME="OB_NS1_60_52_01_18" LAPUADDRESS="518" VERSION="6.1.01.18">
                                <dataTPL UID="27814" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27812" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27810" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27808" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27806" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27804" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27802" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                                <dataTPL UID="27798" NAME="DPS_12_6_52_00_22_VLAN_71-80"/>
                            </sg>
                        </partition>
                    </topology>
                </Action>
            </GetNetworkTopologyResult>
        </GetNetworkTopologyResponse>
    </soap:Body>
</soap:Envelope>'




print_r($xml);  // Return empty . no output
$xml->registerXPathNamespace('GetNetworkTopologyResponse', 'http://tempuri.org/');
foreach ($xml->xpath('//GetNetworkTopologyResponse:GetNetworkTopologyResult') as $item)
{
    print_r($item);
}

请告诉我,我的代码中存在什么问题。我是php的新手。

任何帮助请。

由于

2 个答案:

答案 0 :(得分:1)

有一个警告和一个致命错误。

你有警告的原因是:
您已使用$soap_response,但既未对其进行定义,也未为其分配任何值。

此外,发生致命错误的原因是:
$xml不是对象,因为simplexml_load_string需要第一个字符串参数。

并且您试图在不是对象的变量上调用成员函数。

试试这个

$xmlObj = simplexml_load_string($xml);
$xmlObj->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');
foreach ($xmlObj->xpath('//envoy:payment') as $item){
    print_r($item);
}

答案 1 :(得分:1)

<?php

$xml=simplexml_load_file("text.xml");
print_r($xml);

$xml->registerXPathNamespace('GetNetworkTopologyResponse', 'http://tempuri.org/');
foreach ($xml->xpath('//GetNetworkTopologyResponse:GetNetworkTopologyResult') as $item)
{
print_r($item);
}
?>

这将打印元素数组,现在您可以创建数组分区。

确认您的XML SOAP文件(test.xml)已启动<?XML.....>文件开头没有任何空格,否则会出错: {{1} }“