0

我使用以下方法获取网络服务数据,

$client = new SoapClient("http://empblr.dyndns.org/CentralHomeDelivery_Mob/Service.asmx?wsdl");


     $result = $client->GetAlladdress(array('customerid'=>36));

当我 var_dump $result 我得到,

object(stdClass)[16]
  public 'GetAlladdressResult' => 
    object(stdClass)[17]
      public 'schema' => string '<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table"><xs:complexType><xs:sequence><xs:element name="AdressID" type="xs:int" minOccurs="0"/><xs:element name="CustomerID" type="xs:int" minOccurs="0"/><xs:element name="LocationID" type="xs:int" minOccurs="0"/><xs'... (length=1030)
      public 'any' => string '<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id="Table1" msdata:rowOrder="0"><AdressID>643</AdressID><CustomerID>36</CustomerID><LocationID>176</LocationID><StreetName>asdf</StreetName><HouseNo>1234</HouseNo><AlternatePhone>5632256</AlternatePhone><LandMark>asdf </LandMark><MainLocID>2</MainLocID><locName>ANDOLANA CIRCLE</locName><MainLoc>Mysore    </MainLoc></Table><Table diffgr:id="Table2" m'... (length=1864)

如何从子节点获取值<CustomerID>并存储到数组中?

谢谢..

4

2 回答 2

3

如果您处于 WSDL 模式,通常SoapClient应该提供一个stdClass对象,该对象将这些值作为标准类属性提供。

如果不是这种情况(从你的问题中不清楚,var_dump因为它被切断了)你可以解析你在any属性中的 XML,例如使用SimpleXMLparser

一些代码示例(在线演示):

$xml = new SimpleXMLElement($result->GetAlladdressResult->any);

# traversal
$table = $xml->NewDataSet->Table[0];
echo $table->LocationID, "\n", $table->MainLoc, "\n";

# xpath
echo $xml->xpath('//LocationID')[0], "\n", $xml->xpath('//MainLoc')[0], "\n";

它显示了访问数据的两种替代方法,第一种使用标准遍历,第二种通过xpath()运行 xpath 查询的方法。

于 2013-03-06T05:03:23.630 回答
0

您始终可以使用正则表达式:

preg_match('#<CustomerID>(\d+)</CustomerID>#', $result->GetAlladdressResult->any, $match);
$customerId = isset($match[1]) ? $match[1] : false;
于 2013-03-05T19:03:16.077 回答