从 XML 字符串中检索数据


Retrieve data from XML string

我正在尝试从此XML输出中检索数据:

<string xmlns="http://tempuri.org/soap/AustralianTourismWebService">
<?xml version="1.0" encoding="utf-16" ?><atdw_data_results><area area_id="9000672" area_name="Gippsland area" attribute_id_area_type="LOCAL">
<city suburb_city_postal_code="3962" attribute_id_status="ACTIVE" geocode_gda_latitude="-38.670501000" geocode_gda_longitude="146.395343000" attribute_id_geocode_proj_sys="GDA94">Agnes</city>

能够检索suburb_city_postal_code和attribute_id_status。在这种情况下,如何检索"艾格尼丝"的城市名称?

请在下面查看我的 PHP 代码:

$result = simplexml_load_string(trim(html_entity_decode($result)), 'SimpleXMLElement');
/** Instantiate Loop */
foreach ($result->area->city as $entry) {
$pna = htmlspecialchars_decode($entry->attributes()->suburb_city_postal_code, ENT_QUOTES);
$pna = str_replace("'", "''", $pna);
$str = htmlspecialchars_decode($entry->attributes()->attribute_id_status, ENT_QUOTES);
$str = str_replace("'", "''", $str);
echo  $pna. "<br />";
echo  $str . "<br />";
}

谢谢

从注释扩展:

只需在foreach中使用(string)$entry就可以了:

foreach ($result->area->city as $entry) {
    /* ... */
    echo (string)$entry;
}