Php 通过xml循环并将数据存储到MySQL表

Php 通过xml循环并将数据存储到MySQL表,php,mysql,xml,foreach,Php,Mysql,Xml,Foreach,XML输出如下所示 <?xml version="1.0" encoding="UTF-8"?> <Country code="GR"> <Regions> <Region translation="null">Athens Airport</Region> <Region translation="null">Athens Coast</Region> <Reg

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>
<?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>
如何循环并将其保存在mysql中。。?
TIA

如果.XML输出如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Country>
    <Code></Code>
    <Regions>
        <Region>
            <Name></Name>
            <Cities>
                <City></City>
                <City></City>
                <City></City>
            </Cities>
        </Region>
        <Region>
            <Name></Name>
            <Cities>
                <City></City>
                <City></City>
                <City></City>
            </Cities>
        </Region>
    </Regions>
</Country>
<Country>
    <Code></Code>
    <Regions>
        <Region>
            <Name></Name>
            <Cities>
                <City></City>
                <City></City>
                <City></City>
            </Cities>
        </Region>
        <Region>
            <Name></Name>
            <Cities>
                <City></City>
                <City></City>
                <City></City>
            </Cities>
        </Region>
    </Regions>
</Country>
您应该在php中执行此操作:

$url = "URL which gives an xml output";
$xml = new SimpleXMLElement($url);
foreach($xml->children() as $country){
    // Query to insert the country into the countries table by $country->Code
    foreach($country->Regions as $region){
        // Query to insert the region into the regions table by $country->Code and $region->Name
        foreach($region->Cities as $city){
            // Query to insert the city into the cities table by $country->Code and $region->Name and $city->City
        }
    }
}

这将完全按照要求完成工作。

这是我的XML Alimos Anavyssos Glyfada Lagonissi Palaio Faliro Sounio Voula Vouliagmeni实际上每个地区都有这样的XML输出。不像在XMLi中那样在区域标记内获取FOREACH中的每个区域,并对其进行解析以获取城市的XML输出。现在我需要捕捉每个区域内的所有城市
$url = "URL which gives an xml output";
$xml = new SimpleXMLElement($url);
foreach($xml->children() as $country){
    // Query to insert the country into the countries table by $country->Code
    foreach($country->Regions as $region){
        // Query to insert the region into the regions table by $country->Code and $region->Name
        foreach($region->Cities as $city){
            // Query to insert the city into the cities table by $country->Code and $region->Name and $city->City
        }
    }
}
$regions = array("US" => "USA", "FR" => "France", "AU" => "Australia");
    foreach ($regions as $code => $country) {

        $url = "URL oes here which ives 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;

                    foreach ($region as $RegionName) {

                        $cityURL = "URL which gives city xml data for regions";

                        file_put_contents('Cities.xml', file_get_contents($cityURL));

                        $xml2 = simplexml_load_file("Cities.xml") or die("Error: Cannot create object");

                            foreach ($xml2->children() as $row2) {
                                $city = $row2->City;

                                    foreach ($city as $cityName) {

                                    //Code to add to database

                                    }
                            }
                    }
            }
    }