从XML字符串中检索数据

时间:2013-10-28 07:27:01

标签: php xml

我正在尝试从此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。在这种情况下,如何检索城市名称'Agnes'?

请参阅下面的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 />";

}

谢谢

1 个答案:

答案 0 :(得分:0)

延伸评论:

只需在(string)$entry中使用foreach即可:

foreach ($result->area->city as $entry) {

    /* ... */

    echo (string)$entry;
}