簡體   English   中英

遍歷xml並將數據存儲到MySQL表

[英]Looping through xml and store data to MySQL table

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>

我需要將每個地區和國家/地區下的所有城市插入表格中。 一個國家有很多地區,一個地區有多個城市。 我試過的是

$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

如果您的.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
        }
    }
}
$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

                                    }
                            }
                    }
            }
    }

這將完全按照要求完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM