0

我正在尝试用 php 阅读这个 XML 文档。

https://raw.github.com/yosoyadri/GeoNames-XML-Builder/master/continents-countries-statesprovinces.xml

这就是我到目前为止所知道的。

$xml = new SimpleXMLElement(file_get_contents(TEMPLATEPATH.'/lib/regions.xml'));
echo $xml->Children[0]->SimpleGeoName[0]->Id;

我不知道如何遍历所有国家并使用 php 存储它们。我对 xml 解析真的很陌生,我什至不确定我是否能够遍历所有这样的结果。因为$xml->Children->..返回空。

任何帮助都非常感谢。

4

3 回答 3

1

这就是你循环的方式xml

echo "<pre>";
$xml = new SimpleXMLElement(file_get_contents("regions.xml"));
foreach ( $xml->Children as $region ) { // iterate through all the regions
    foreach ( $region as $continent ) {
        echo "Continet ID :", $continent->Id . PHP_EOL;
        echo "Continet Name :", $continent->Name . PHP_EOL;

        echo PHP_EOL;

        foreach ( $continent->Children as $countryList ) {
            foreach ( $countryList as $country ) {
                // var_dump($country);
                echo "\tCountry ID :", $country->Id . PHP_EOL;
                echo "\tCountry Name  :", $country->Name . PHP_EOL;

                echo PHP_EOL;
            }
        }
    }
}

输出

Continet ID :6255146
Continet Name :Africa

    Country ID :2589581
    Country Name  :Algeria

    Country ID :3351879
    Country Name  :Angola

    Country ID :2395170
    Country Name  :Benin

    Country ID :933860
    Country Name  :Botswana

    Country ID :2361809
    Country Name  :Burkina Faso

    Country ID :433561
    Country Name  :Burundi

    Country ID :2233387
    Country Name  :Cameroon

    Country ID :3374766
    Country Name  :Cape Verde

    Country ID :239880
    Country Name  :Central African Republic

    Country ID :2434508
    Country Name  :Chad

    Country ID :921929
    Country Name  :Comoros

    Country ID :203312
    Country Name  :Congo

    Country ID :223816
    Country Name  :Djibouti

    Country ID :357994
    Country Name  :Egypt

    Country ID :2309096
    Country Name  :Equatorial Guinea

    Country ID :338010
    Country Name  :Eritrea

    Country ID :337996
    Country Name  :Ethiopia

    Country ID :2400553
    Country Name  :Gabon

    Country ID :2413451
    Country Name  :Gambia

    Country ID :2300660
    Country Name  :Ghana

    Country ID :2420477
    Country Name  :Guinea

    Country ID :2372248
    Country Name  :Guinea-Bissau

    Country ID :2287781
    Country Name  :Ivory Coast

    Country ID :192950
    Country Name  :Kenya

    Country ID :932692
    Country Name  :Lesotho

    Country ID :2275384
    Country Name  :Liberia

    Country ID :2215636
    Country Name  :Libya

    Country ID :1062947
    Country Name  :Madagascar

    Country ID :927384
    Country Name  :Malawi

    Country ID :2453866
    Country Name  :Mali

    Country ID :2378080
    Country Name  :Mauritania

    Country ID :934292
    Country Name  :Mauritius

    Country ID :1024031
    Country Name  :Mayotte

    Country ID :2542007
    Country Name  :Morocco

    Country ID :1036973
    Country Name  :Mozambique

    Country ID :3355338
    Country Name  :Namibia

    Country ID :2440476
    Country Name  :Niger

    Country ID :2328926
    Country Name  :Nigeria

    Country ID :2260494
    Country Name  :Republic of the Congo

    Country ID :49518
    Country Name  :Rwanda

    Country ID :935317
    Country Name  :Réunion

    Country ID :3370751
    Country Name  :Saint Helena

    Country ID :2245662
    Country Name  :Senegal

    Country ID :241170
    Country Name  :Seychelles

    Country ID :2403846
    Country Name  :Sierra Leone

    Country ID :51537
    Country Name  :Somalia

    Country ID :953987
    Country Name  :South Africa

    Country ID :7909807
    Country Name  :South Sudan

    Country ID :366755
    Country Name  :Sudan

    Country ID :934841
    Country Name  :Swaziland

    Country ID :2410758
    Country Name  :São Tomé and Príncipe

    Country ID :149590
    Country Name  :Tanzania

    Country ID :2363686
    Country Name  :Togo

    Country ID :2464461
    Country Name  :Tunisia

    Country ID :226074
    Country Name  :Uganda

    Country ID :2461445
    Country Name  :Western Sahara

    Country ID :895949
    Country Name  :Zambia

    Country ID :878675
    Country Name  :Zimbabwe

Continet ID :6255152
Continet Name :Antarctica

    Country ID :6697173
    Country Name  :Antarctica

    Country ID :3371123
    Country Name  :Bouvet Island

    Country ID :1546748
    Country Name  :French Southern Territories

    Country ID :1547314
    Country Name  :Heard Island and McDonald Islands

    Country ID :3474415
    Country Name  :South Georgia and the South Sandwich Islands

Continet ID :6255147
Continet Name :Asia

    Country ID :1149361
    Country Name  :Afghanistan

    Country ID :174982
    Country Name  :Armenia

    Country ID :587116
    Country Name  :Azerbaijan

    Country ID :290291
    Country Name  :Bahrain

    Country ID :1210997
    Country Name  :Bangladesh

    Country ID :1252634
    Country Name  :Bhutan

    Country ID :1282588
    Country Name  :British Indian Ocean Territory

    Country ID :1820814
    Country Name  :Brunei

    Country ID :1831722
    Country Name  :Cambodia

    Country ID :1814991
    Country Name  :China

    Country ID :2078138
    Country Name  :Christmas Island

    Country ID :1547376
    Country Name  :Cocos [Keeling] Islands

    Country ID :614540
    Country Name  :Georgia

    Country ID :1819730
    Country Name  :Hong Kong

    Country ID :1269750
    Country Name  :India

    Country ID :1643084
    Country Name  :Indonesia

    Country ID :130758
    Country Name  :Iran

    Country ID :99237
    Country Name  :Iraq

    Country ID :294640
    Country Name  :Israel

    Country ID :1861060
    Country Name  :Japan

    Country ID :248816
    Country Name  :Jordan

    Country ID :1522867
    Country Name  :Kazakhstan

    Country ID :285570
    Country Name  :Kuwait

    Country ID :1527747
    Country Name  :Kyrgyzstan

    Country ID :1655842
    Country Name  :Laos

    Country ID :272103
    Country Name  :Lebanon

    Country ID :1821275
    Country Name  :Macao

    Country ID :1733045
    Country Name  :Malaysia

    Country ID :1282028
    Country Name  :Maldives

    Country ID :2029969
    Country Name  :Mongolia

    Country ID :1327865
    Country Name  :Myanmar [Burma]

    Country ID :1282988
    Country Name  :Nepal

    Country ID :1873107
    Country Name  :North Korea

    Country ID :286963
    Country Name  :Oman

    Country ID :1168579
    Country Name  :Pakistan

    Country ID :6254930
    Country Name  :Palestine

    Country ID :1694008
    Country Name  :Philippines

    Country ID :289688
    Country Name  :Qatar

    Country ID :102358
    Country Name  :Saudi Arabia

    Country ID :1880251
    Country Name  :Singapore

    Country ID :1835841
    Country Name  :South Korea

    Country ID :1227603
    Country Name  :Sri Lanka

    Country ID :163843
    Country Name  :Syria

    Country ID :1668284
    Country Name  :Taiwan

    Country ID :1220409
    Country Name  :Tajikistan

    Country ID :1605651
    Country Name  :Thailand

    Country ID :298795
    Country Name  :Turkey

    Country ID :1218197
    Country Name  :Turkmenistan

    Country ID :290557
    Country Name  :United Arab Emirates

    Country ID :1512440
    Country Name  :Uzbekistan

    Country ID :1562822
    Country Name  :Vietnam

    Country ID :69543
    Country Name  :Yemen

Continet ID :6255148
Continet Name :Europe

    Country ID :783754
    Country Name  :Albania

    Country ID :3041565
    Country Name  :Andorra

    Country ID :2782113
    Country Name  :Austria

    Country ID :630336
    Country Name  :Belarus

    Country ID :2802361
    Country Name  :Belgium

    Country ID :3277605
    Country Name  :Bosnia and Herzegovina

    Country ID :732800
    Country Name  :Bulgaria

    Country ID :3202326
    Country Name  :Croatia

    Country ID :146669
    Country Name  :Cyprus

    Country ID :3077311
    Country Name  :Czech Republic

    Country ID :2623032
    Country Name  :Denmark

    Country ID :453733
    Country Name  :Estonia

    Country ID :2622320
    Country Name  :Faroe Islands

    Country ID :660013
    Country Name  :Finland

    Country ID :3017382
    Country Name  :France

    Country ID :2921044
    Country Name  :Germany

    Country ID :2411586
    Country Name  :Gibraltar

    Country ID :390903
    Country Name  :Greece

    Country ID :719819
    Country Name  :Hungary

    Country ID :2629691
    Country Name  :Iceland

    Country ID :2963597
    Country Name  :Ireland

    Country ID :3175395
    Country Name  :Italy

    Country ID :831053
    Country Name  :Kosovo

    Country ID :458258
    Country Name  :Latvia

    Country ID :3042058
    Country Name  :Liechtenstein

    Country ID :597427
    Country Name  :Lithuania

    Country ID :2960313
    Country Name  :Luxembourg

    Country ID :718075
    Country Name  :Macedonia

    Country ID :2562770
    Country Name  :Malta

    Country ID :617790
    Country Name  :Moldova

    Country ID :2993457
    Country Name  :Monaco

    Country ID :3194884
    Country Name  :Montenegro

    Country ID :2750405
    Country Name  :Netherlands

    Country ID :3144096
    Country Name  :Norway

    Country ID :798544
    Country Name  :Poland

    Country ID :2264397
    Country Name  :Portugal

    Country ID :798549
    Country Name  :Romania

    Country ID :2017370
    Country Name  :Russia

    Country ID :3168068
    Country Name  :San Marino

    Country ID :6290252
    Country Name  :Serbia

    Country ID :3057568
    Country Name  :Slovakia

    Country ID :3190538
    Country Name  :Slovenia

    Country ID :2510769
    Country Name  :Spain

    Country ID :607072
    Country Name  :Svalbard and Jan Mayen

    Country ID :2661886
    Country Name  :Sweden

    Country ID :2658434
    Country Name  :Switzerland

    Country ID :690791
    Country Name  :Ukraine

    Country ID :2635167
    Country Name  :United Kingdom

    Country ID :3164670
    Country Name  :Vatican City

    Country ID :661882
    Country Name  :Ã…land

Continet ID :6255149
Continet Name :North America

    Country ID :3573511
    Country Name  :Anguilla

    Country ID :3576396
    Country Name  :Antigua and Barbuda

    Country ID :3577279
    Country Name  :Aruba

    Country ID :3572887
    Country Name  :Bahamas

    Country ID :3374084
    Country Name  :Barbados

    Country ID :3582678
    Country Name  :Belize

    Country ID :3573345
    Country Name  :Bermuda

    Country ID :7626844
    Country Name  :Bonaire

    Country ID :3577718
    Country Name  :British Virgin Islands

    Country ID :6251999
    Country Name  :Canada

    Country ID :3580718
    Country Name  :Cayman Islands

    Country ID :3624060
    Country Name  :Costa Rica

    Country ID :3562981
    Country Name  :Cuba

    Country ID :7626836
    Country Name  :Curacao

    Country ID :3575830
    Country Name  :Dominica

    Country ID :3508796
    Country Name  :Dominican Republic

    Country ID :3585968
    Country Name  :El Salvador

    Country ID :3425505
    Country Name  :Greenland

    Country ID :3580239
    Country Name  :Grenada

    Country ID :3579143
    Country Name  :Guadeloupe

    Country ID :3595528
    Country Name  :Guatemala

    Country ID :3723988
    Country Name  :Haiti

    Country ID :3608932
    Country Name  :Honduras

    Country ID :3489940
    Country Name  :Jamaica

    Country ID :3570311
    Country Name  :Martinique

    Country ID :3996063
    Country Name  :Mexico

    Country ID :3578097
    Country Name  :Montserrat

    Country ID :3617476
    Country Name  :Nicaragua

    Country ID :3703430
    Country Name  :Panama

    Country ID :4566966
    Country Name  :Puerto Rico

    Country ID :3578476
    Country Name  :Saint Barthélemy

    Country ID :3575174
    Country Name  :Saint Kitts and Nevis

    Country ID :3576468
    Country Name  :Saint Lucia

    Country ID :3578421
    Country Name  :Saint Martin

    Country ID :3424932
    Country Name  :Saint Pierre and Miquelon

    Country ID :3577815
    Country Name  :Saint Vincent and the Grenadines

    Country ID :7609695
    Country Name  :Sint Maarten

    Country ID :3573591
    Country Name  :Trinidad and Tobago

    Country ID :3576916
    Country Name  :Turks and Caicos Islands

    Country ID :4796775
    Country Name  :U.S. Virgin Islands

    Country ID :6252001
    Country Name  :United States

Continet ID :6255151
Continet Name :Oceania

    Country ID :5880801
    Country Name  :American Samoa

    Country ID :2077456
    Country Name  :Australia

    Country ID :1899402
    Country Name  :Cook Islands

    Country ID :2170371
    Country Name  :Coral Sea Islands Territory

    Country ID :1966436
    Country Name  :East Timor

    Country ID :2205218
    Country Name  :Fiji

    Country ID :4030656
    Country Name  :French Polynesia

    Country ID :4043988
    Country Name  :Guam

    Country ID :4030945
    Country Name  :Kiribati

    Country ID :2080185
    Country Name  :Marshall Islands

    Country ID :2081918
    Country Name  :Micronesia

    Country ID :2110425
    Country Name  :Nauru

    Country ID :2139685
    Country Name  :New Caledonia

    Country ID :2186224
    Country Name  :New Zealand

    Country ID :4036232
    Country Name  :Niue

    Country ID :2155115
    Country Name  :Norfolk Island

    Country ID :4041468
    Country Name  :Northern Mariana Islands

    Country ID :1559582
    Country Name  :Palau

    Country ID :2088628
    Country Name  :Papua New Guinea

    Country ID :4030699
    Country Name  :Pitcairn Islands

    Country ID :4034894
    Country Name  :Samoa

    Country ID :2103350
    Country Name  :Solomon Islands

    Country ID :2077507
    Country Name  :Territory of Ashmore and Cartier Islands

    Country ID :4031074
    Country Name  :Tokelau

    Country ID :4032283
    Country Name  :Tonga

    Country ID :2110297
    Country Name  :Tuvalu

    Country ID :5854968
    Country Name  :U.S. Minor Outlying Islands

    Country ID :2134431
    Country Name  :Vanuatu

    Country ID :4034749
    Country Name  :Wallis and Futuna

Continet ID :6255150
Continet Name :South America

    Country ID :3865483
    Country Name  :Argentina

    Country ID :3923057
    Country Name  :Bolivia

    Country ID :3469034
    Country Name  :Brazil

    Country ID :3895114
    Country Name  :Chile

    Country ID :3686110
    Country Name  :Colombia

    Country ID :3658394
    Country Name  :Ecuador

    Country ID :3474414
    Country Name  :Falkland Islands

    Country ID :3381670
    Country Name  :French Guiana

    Country ID :3378535
    Country Name  :Guyana

    Country ID :3437598
    Country Name  :Paraguay

    Country ID :3932488
    Country Name  :Peru

    Country ID :3382998
    Country Name  :Suriname

    Country ID :3439705
    Country Name  :Uruguay

    Country ID :3625428
    Country Name  :Venezuela
于 2012-09-21T18:21:52.193 回答
0

SimpleXMLElement并不是那么简单,事实上只有当你猜测如何处理它时它才会变得简单......或者有人建议你。

假设regions.xml是这样的:

<regions>
    <region id="01" size="big">
        New Zealand
    </region>
    <region id="02" size="small">
        Tokelau
    </region>
</regions>

以下 PHP 代码可以浏览该 XML 文档:

$xml = new SimpleXMLElement('regions.xml');
foreach ($xml->region as $region) { // iterate through all the regions
    echo 'Region ID: '. (string)$region['id']; // get the attribute id
    echo 'Region size: '. (string)$region['size']; // get the attribute size
    echo 'Region name: '. (string)$region; // get the contents of the
                                           // element by casting it to a string
}

现在让我们做一些更难的事情......假设<region>有一个子元素,<subregion>

<regions>
    <region id="01" size="big">
        <name>USA</name>
        <subregion id="01_1">Alaska</subregion>
    </region>
    ...
</regions>

如果要获取每个区域的所有子区域,则必须这样做:

$xml = new SimpleXMLElement('regions.xml');
foreach ($xml->region as $region) { // iterate through all the regions
    foreach ($region->subregion as $subregion) // iterate trough the subregion of $region
       // do something
    }
}

您的文档结构似乎比这复杂一点,但使用这些基础知识您可以轻松解决。

于 2012-09-21T18:00:19.067 回答
0

您的 xml 值现在是一个 php 对象。你可以跑

var_dump($xml);
exit;

这将打印出发送给您的任何极其复杂的 xml 对象结构。然后,您将能够查看要在哪个子对象上运行 foreach 循环以获得仅包含国家/地区名称的列表。祝你好运。

于 2012-09-21T17:54:54.533 回答