【问题标题】:Looping through xml and store data to MySQL table循环遍历xml并将数据存储到MySQL表中
【发布时间】: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

【问题讨论】:

    标签: php mysql xml foreach


    【解决方案1】:

    如果你的 .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 AlimosAnavyssosGlyfadaLagonissiPalaio FaliroSounioVoula Vouliagmeni 实际上每个地区都有这样的 XML 输出。不在您的 XML 中的区域标签内
    • 我在 FOREACH 中获取每个区域并对其进行解析以获取城市的 XML 输出。现在我需要捕获每个区域内的所有城市
    【解决方案2】:
    $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
    
                                        }
                                }
                        }
                }
        }
    

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

    【讨论】:

      猜你喜欢
      • 2020-03-02
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2017-01-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-05
      相关资源
      最近更新 更多