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

在php中显示响应xml字符串的节点,php,xml,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)

我从远程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->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中的城市名称。提前感谢。

我使用以下代码在城市节点之间循环:

<?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在大文件上的速度要快得多。

向我们展示完整的PHP.PHP代码???或者返回的字符串?使用foreach循环.trued-foreach$parser->City->Name作为$n{echo$n;}但是它只显示它的第一个城市名。但我不明白为什么PHP_EOL在循环中。。?它在没有PHP_EOL的情况下也可以工作。PHP_EOL只是新行的一个常量。在html中,您可以使用br标记。