【问题标题】:display nodes of response xml string in php在php中显示响应xml字符串的节点
【发布时间】: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-&lt;asXML()); 而不是var_dump() 并使用结果编辑您的问题。

标签: php xml


【解决方案1】:

我正在使用以下代码循环遍历 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 在处理大文件时要快得多。

【讨论】:

  • 非常感谢它的工作。但我不明白为什么 PHP_EOL 在循环中..?它也可以在没有 PHP_EOL 的情况下工作。
  • PHP_EOL 只是换行的常量。在 html 中,您可以使用 br 标记。
猜你喜欢
  • 1970-01-01
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多