在php中显示响应xml字符串的节点

时间:2014-03-15 06:12:14

标签: php xml

我收到来自远程webservice的响应为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中仅显示特定节点(例如,仅考虑城市的名称)。任何建议或例子非常赞赏。

获取此响应的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-&gt; 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中仅显示特定节点(例如,仅考虑城市名称)。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我使用以下代码循环遍历City节点:

<?php

$content = file_get_contents(__DIR__ . '/city.xml');

$parser = simplexml_load_string($content);

foreach ($parser->City as $subnode) {
    echo $subnode->Name . PHP_EOL;
}

假设XML看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<test>
<City>
    <CityId>1</CityId>
    <Name>South Park</Name>
    <Currency>Dollar</Currency>
</City>
<City>
    <CityId>1</CityId>
    <Name>South Park</Name>
    <Currency>Dollar</Currency>
</City>
<City>
    <CityId>1</CityId>
    <Name>South Park</Name>
    <Currency>Dollar</Currency>
</City>
</test>

但是我建议使用XMLReader来遍历xml,因为SimpleXml在错误方面比较差,而且我认为XMLReader在大文件上要快得多。