2012-09-21 4 views
0

나는이 XML 문서를 PHP로 읽으려고한다. 지금까지 알아 낸 것입니다SimpleXMLElement를 사용하여 XML 파일의 모든 결과를 PHP 배열/루프로 구문 분석 할 수 있습니까?

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->..이 비어 있기 때문에

도움을 주시면 감사하겠습니다.

+1

[기본 SimpleXML을 사용 (http://php.net/manual/en/simplexml.examples-basic. PHP) – Gordon

답변

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 
0

귀하의 xml 값은 현재 PHP 개체입니다. 당신은 실행할 수 있습니다

var_dump($xml); 
exit; 

그리고 그게 뭐든간에 복잡한 xml 개체 구조가 당신에게 전송됩니다 인쇄됩니다. 그러면 나라 이름 목록을 얻기 위해 foreach 루프를 실행할 하위 오브젝트를 볼 수 있습니다. 행운을 빕니다.

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 
    } 
} 

귀하의 문서 구조는 좀 더 복잡한 이것보다 보이지만, 당신이 할 수있는 이러한 기본을 사용하여 쉽게 해결할 수 있습니다.

관련 문제