【发布时间】:2018-10-19 03:51:13
【问题描述】:
XML输出如下
<?xml version="1.0" encoding="UTF-8"?>
<Country code="GR">
<Regions>
<Region translation="null">Athens Airport</Region>
<Region translation="null">Athens Coast</Region>
<Region translation="null">Athens Suburbs-Attica</Region>
<Region translation="null">Athens</Region>
<Region translation="null">Central Greece-Etoloakarnania</Region>
<Region translation="null">Central Greece-Evritania</Region>
<Region translation="null">Central Greece-Ioannina</Region>
<Region translation="null">Central Greece-Karditsa</Region>
<Region translation="null">Central Greece-Larissa</Region>
<Region translation="null">Central Greece-Magnissia</Region>
</Regions>
</Country>
每个Region都有城市,如下
<?xml version="1.0" encoding="UTF-8"?>
<Country code="GR">
<Cities>
<City translation="null">Acharnes</City>
<City translation="null">Achladies</City>
<City translation="null">Achladochori</City>
<City translation="null">Adamas</City>
<City translation="null">Afandou</City>
<City translation="null">Afiartis</City>
<City translation="null">Agali</City>
<City translation="null">Aghia Anna</City>
<City translation="null">Aghia Paraskevi</City>
</Cities>
我需要将每个地区和国家下的所有城市插入到一个表中。 一个国家有多个地区,一个地区有多个城市。 我尝试的是
$regions = array("GR" => "Greece", "BR" => "Brazil", "US" => "USA");
foreach ($regions as $code => $country) {
$url = "URL which gives an xml output"
file_put_contents($code . '.xml', file_get_contents($url));
$xml = simplexml_load_file($code".xml") or die("Error: Cannot create object");
foreach ($xml->children() as $row) {
$region = $row->Region;
}
}
如何循环并保存在 mysql..? TIA
【问题讨论】: