如何使用 PHP 在 XML 文件根节点上获取钩子


How do I get a hook on an XML files root node using PHP?

我有以下XML文件(如下),我想在根节点的名称上找到一个钩子。

我尝试了以下方法:

$config = simplexml_load_string($xml_data);
echo $config->getName();

哪个回声什么都不是。我也迭代了结果,但它从根节点的子节点开始

foreach($config as $key => $value)
{
    echo $key;
}
// echo's ReturnStatus, SearchURL and PropertyResults

我只想要根节点的名称"搜索响应"。我似乎在 SimpleXMLElement 类文档中找不到任何有助于 http://www.php.net/manual/en/class.simplexmlelement.php

<?xml version="1.0" encoding="ISO-8859-1" ?>
<SearchResponse>
  <ReturnStatus>
    <Success>
        <data>true</data>
        <data2>true</data2>
    </Success>
    <Success>
        <data>false</data>
        <data2>true</data2>
    </Success>
    <Exception />
  </ReturnStatus>
  <SearchURL />
  <PropertyResults>
    <PropertyResult>
      <PropertyID>1468830</PropertyID>
      <PropertyName>Sands Acapulco</PropertyName>
    </PropertyResult>
    <PropertyResult>
      <PropertyID>1353302</PropertyID>
      <PropertyName>Acapulco Turquesa</PropertyName>
    </PropertyResult>
    <PropertyResult>
      <PropertyID>4521565</PropertyID>
      <PropertyName>Almaria Delsonto</PropertyName>
    </PropertyResult>   
  </PropertyResults>
</SearchResponse>

这确实有效:

$config = simplexml_load_string($xml);
echo $config->getName();

使用您提供的 XML 进行测试后,输出:

SearchResponse

你写它不起作用,所以问题可能不是 API。