如何从stdClass对象数组中获取的xml对象中检索组合框中的国家/地区名称列表?

时间:2014-12-23 14:50:22

标签: php arrays soap

问题:我有来自SOAP请求的响应,如下所示,其中 CountryList 响应是具有CountryCode&的XML对象。国家的名字。那么如何从结果中显示具有CountryName的combox框呢?

stdClass Object
(
[CountryList] => 
AFAfghanistanALAlbaniaDZAlgeriaASAmerican  amoaADAndorraAOAngolaAIAnguillaAQAntarcticaAGAntigua & BarbudaARArgentinaAMArmeniaAWArubaAUAustraliaATAustriaAZAzerbaijanBSBahamasBHBahrainBDBangladeshBBBarbadosBYBelarus(Belorussia)BEBelgiumBZBelizeBJBeninBMBermudaBTBhutanBOBoliviaBABosnia and HerzegowinaBWBotswanaBVBouvet IslandsBRBrazilIOBritish Indian Ocean TerritoryVGBritish Virgin IslandsBNBrunei DarussalamBGBulgariaBFBurkina FasoBIBurundiKHCambodiaCMCameroonCACanadaCBCanada BufferCVCape VerdeKYCayman IslandsCFCentral African RepublicTDChadCLChileCNChinaCXChristmas IslandsCCCocos (Keeling) IslandCOColombiaKMComorosCGCongoCDCongo (Rep. Dem.)CKCook IslandsCRCosta RicaHRCroatiaCUCubaCYCyprusCZCzech RepublicDKDenmarkDJDjiboutiDODominican RepublicDMDominicanaTPEast TimorECEcuadorEGEgyptSVEl SalvadorGQEquatorial GuineaEREritreaEEEstoniaETEthiopiaEUEuropean Monetary UnionFKFalkland IslandsFOFaroe IslandsFJFiji IslandsFIFinlandFRFranceGFFrench GuianaPFFrench PolynesiaTFFrench Southern TerritoriesGAGabonGMGambiaGEGeorgiaDEGermanyGHGhanaGIGibralterGRGreeceGLGreenlandGDGrenadaGPGuadeloupeGUGuamGTGuatemalaGNGuineaGWGuinea-BissauGYGuyanaHTHaitiHMHeard & Mcdonald IslandsHNHondurasHKHongkongHUHungaryISIcelandINIndiaIDIndonesiaIRIranIQIraqIEIrelandILIsraelITItalyCIIvory CoastJMJamaicaJPJapanJOJordanKZKazakhstanKEKenyaKIKiribatiKPKorea (Democratic People's Republic Of)KWKuwaitKGKyrgyzstanLALao People's Democratic RepublicLVLatviaLBLebanonLSLesothoLRLiberiaLYLibyan Arab JamahiriyaLILiechtensteinLTLithuaniaQLLithuania (Dummy Code)LULuxembourgMOMacauMKMacedoniaMGMadagascarMWMalawiMYMalaysiaMVMaldivesMLMaliMTMaltaMHMarshall IslandsMQMartiniqueMRMauritaniaMUMauritiusYTMayotteMXMexicoMBMexico BufferFMMicronesiaMDMoldovaMCMonacoMNMongoliaMSMontserratMAMoroccoMZMozambiqueMMMyanmarNANamibiaNRNauruNPNepalNLNetherlandsANNetherlands AntillesNCNew CaledoniaNZNew ZealandNINicaraguaNENigerNGNigeriaNUNiueNFNorfolk IslandsMPNorthern Mariana IslandsNONorwayOMOmanPKPakistanPWPalauPSPalestinian Occ. TerritoriesPAPanamaPGPapua New GuineaPYParaguayPEPeruPHPhilippinesPLPolandPTPortugalPRPuerto RicoQAQatarREReunionRORomaniaRWRuandaRURussian FederationLCSaint LuciaWSSamoaSMSan MarinoSTSao Tome & PrincipeSASaudi ArabiaSNSenegalSCSeychellesSLSierra LeoneSGSingaporeSKSlovakiaSISloveniaSBSolomon IslandsSOSomaliaZASouth AfricaGSSouth Georgia & South SandwichKRSouth KoreaSUSoviet UnionESSpainLKSri LankaSHSt. HelenaKNSt. Kitts and NevisPMSt. Pierre & MiquelonVCSt. Vincent & the GrenadinesSDSudanSRSurinameSJSvalbard & Jan Mayen IslandsSZSwazilandSESwedenCHSwitzerlandSYSyrian Arab RepublicTWTaiwanTJTajikistanTZTanzaniaTHThailandTGTogoTKTokelauTOTongaTTTrinidad and TobagoTNTunisiaTCTurcs & Caicos IslandsTRTurkeyTMTurkmenistanTVTuvaluUMU.S. Minor Outlaying IslandsUGUgandaUAUkraineAEUnited Arab EmiratesGBUnited Kingdom UYUruguay USUSAUZUzbekistan VUVanuatuVAVatican City StateVEVenezuelaVNVietnamVIVirgin Islands (US)WFWallis & Futuna IslandsEHWestern SaharaYEYemenYUYugoslaviaZMZambiaZWZimbabwe
[Error] => stdClass Object
(
[ErrorCode] => 0
[ErrorMessage] =>
)
[Status] => 1
[TokenId] => 1d49ba03-173f-49a9-ad07-b758b0ea2fc9
)

2 个答案:

答案 0 :(得分:0)

使用您提供给我们的字符串,您可以执行此操作。

$str = "<Countries><Country><Code>AF</Code><Name>Afghanistan</Name></Coun‌​try><Country><Code>AL</Code><Name>Albania</Name></Country><Country><Code>DZ</Code‌​> <Name>Algeria</Name></Country><Country><Code>AS</Code><Name>American Samoa</Name></Country></Countries>";

$str = substr_delimeters($str, "<Countries>", "</Countries>");
$countries = explode("</Country>", $str);

echo "<select name='countries'>";

foreach($countries as $country)
{           
    if($country != '')
    {
        $country_name = substr_delimeters($country, "<Name>", "</Name>");

        $country_abrev = substr_delimeters($country, "<Code>", "</Code>");        

        echo "<option value='" . $country_abrev . "'>" . $country_name . "</option>";
    }
}    

echo "</select>";

function substr_delimeters($inputstr,$delimeterLeft,$delimeterRight) {
    if (empty($inputstr) || empty($delimeterLeft) || empty($delimeterRight)) return false;
    $posLeft  = stripos($inputstr,$delimeterLeft)+strlen($delimeterLeft);
    $posRight = stripos($inputstr,$delimeterRight,$posLeft+1);
    if (($posLeft===false) || ($posRight===false)) return false;
    return  substr($inputstr,$posLeft,$posRight-$posLeft);
}

答案 1 :(得分:0)

或者你可以使用这样的东西

$string = "<Countries><Country><Code>AF</Code><Name>Afghanistan</Name></Coun‌​try><Country><Code>AL</Code><Name>Albania</Name></Country><Country><Code>DZ</Code‌​> <Name>Algeria</Name></Country><Country><Code>AS</Code><Name>American Samoa</Name></Country></Countries>";
$dom = new DOMDocument();
$dom->loadXml($string);
$countries = $dom->getElementsByTagName('Country');
echo '<select name="countries">';
foreach($countries as $country) {
    $code = $country->getElementsByTagName('Code')->item(0)->nodeValue;
    $countryName = $country->getElementsByTagName('Name')->item(0)->nodeValue;
    echo sprintf('<option value="%s">%s</option>', $code, $countryName);
}
echo '</select>';