【发布时间】:2014-03-15 06:12:13
【问题描述】:
我收到了来自远程 Web 服务的 xml 字符串响应,该字符串在浏览器中如下所示,带有 var_dump($parser)。
object(SimpleXMLElement)[1]
public 'City' =>
array (size=374)
0 =>
object(SimpleXMLElement)[2]
public 'CityID' => string '421' (length=3)
public 'Name' => string 'Abbadia San Salvatore' (length=21)
public 'Currency' => string 'EUR' (length=3)
public 'AreaUnit' => string 'SquareMeter' (length=11)
public 'CountryID' => string '93' (length=2)
1 =>
object(SimpleXMLElement)[3]
public 'CityID' => string '423' (length=3)
public 'Name' => string 'Aberdeen' (length=8)
public 'Currency' => string 'GBP' (length=3)
public 'AreaUnit' => string 'SquareMeter' (length=11)
public 'CountryID' => string '216' (length=3)
2 =>
object(SimpleXMLElement)[4]
public 'CityID' => string '9360' (length=4)
public 'Name' => string 'Aeolian Islands' (length=15)
public 'Currency' => string 'EUR' (length=3)
public 'AreaUnit' => string 'SquareMeter' (length=11)
public 'CountryID' => string '93' (length=2)
3 =>
object(SimpleXMLElement)[5]
public 'CityID' => string '480' (length=3)
public 'Name' => string 'Agia Napa' (length=9)
public 'Currency' => string 'EUR' (length=3)
public 'AreaUnit' => string 'SquareMeter' (length=11)
public 'CountryID' => string '48' (length=2)
4 =>
object(SimpleXMLElement)[6]
public 'CityID' => string '151' (length=3)
public 'Name' => string 'Agrigento' (length=9)
public 'Currency' => string 'EUR' (length=3)
public 'AreaUnit' => string 'SquareMeter' (length=11)
public 'CountryID' => string '93' (length=2)
依此类推,它总共包含 374 个条目。我的问题是如何在 php.ini 中仅显示特定节点(例如仅考虑城市名称)。非常感谢任何建议或示例。
获取此响应的php代码如下:
<?php
$User = "XXXXXXXXXXX";
$Password = "XXXXXXXXXXXXXX";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_URL,
'http://www.mywebservices.com/webservice/clientservice/Version2/Service_partner.asmx/GetCities?Partner='.$User.'&Password='.$Password.''
);
$content = curl_exec($ch);
curl_close($ch);
$parser = simplexml_load_string($content);
var_dump($parser);
?>
@michi 根据您的建议,我已经完成了 - echo htmlentities($parser->asXML());而不是 var_dump()。结果如下:
<?xml version="1.0" encoding="utf-8"?> <ArrayOfCity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.mywebservice.com/otadbnet/service/"> <City> <CityID>421</CityID> <Name>Abbadia San Salvatore</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>93</CountryID> </City> <City> <CityID>423</CityID> <Name>Aberdeen</Name> <Currency>GBP</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>216</CountryID> </City> <City> <CityID>9360</CityID> <Name>Aeolian Islands</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>93</CountryID> </City> <City> <CityID>480</CityID> <Name>Agia Napa</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>48</CountryID> </City> <City> <CityID>151</CityID> <Name>Agrigento</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>93</CountryID> </City> <City> <CityID>499</CityID> <Name>Aguilas</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>178</CountryID> </City> <City> <CityID>517</CityID> <Name>Aix-en-Provence</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>66</CountryID> </City> <City> <CityID>540</CityID> <Name>Alanya</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>209</CountryID> </City> <City> <CityID>561</CityID> <Name>Albufeira</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>155</CountryID> </City> <City> <CityID>577</CityID> <Name>Alcossebre</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>178</CountryID> </City> <City> <CityID>192</CityID> <Name>Algarve</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>155</CountryID> </City> <City> <CityID>606</CityID> <Name>Alicante</Name> <Currency>EUR</Currency> <AreaUnit>SquareMeter</AreaUnit> <CountryID>178</CountryID> </City> ..... and so on.
请进一步告知如何在 php.ini 中仅显示特定节点(例如仅考虑城市名称)。提前致谢。
【问题讨论】:
-
向我们展示完整的代码 (PHP)。
-
php代码???还是返回字符串?
-
使用
foreach循环。 -
试过 -- foreach($parser->City->Name as $n){ echo $n; } 但它只显示第一个城市名称。
-
执行
echo htmlentities($parser-<asXML());而不是var_dump()并使用结果编辑您的问题。