代码之家  ›  专栏  ›  技术社区  ›  Hubert Solecki

从SimpleXMLElement对象获取值

  •  1
  • Hubert Solecki  · 技术社区  · 12 年前

    我写这篇文章是因为我试图解决一个看起来很愚蠢但不幸的是我做不到的问题 simplexml_load_file($link) 如下所示:

    function getListPoints($countryCode,$nbrPoints)
    {
    
    $xml_containt_url = "http://openchargemap.org/api/?output=xml&countrycode=".$countryCode."&maxresults=".$nbrPoints;
    
    $xml_output = simplexml_load_file($xml_containt_url);
    
    
    return $xml_output;
    }
    

    因此,当我打印xml输出时:

    $infos_point = getListPoints("US",2);
    print($infos_point);
    

    我得到了我想要的一切,它给了我:

    SimpleXMLElement Object
    
    (
    
    [ChargePoint] => Array
    
        (
    
            [0] => SimpleXMLElement Object
    
                (
                    [@attributes] => Array
                        (
                            [ID] => 2381
                            [UUID] => BFE199D5-07D4-4310-86D7-8BCB9092541D
                            [DateLastConfirmed] => 31/08/2010 00:00:00
                            [OperatorID] => 1
                            [OperatorTitle] => (Unknown Operator)
                            [DataProviderID] => 2
                            [NumberOfPoints] => 
                            [DataQualityLevel] => 1
                            [PercentageSimilarity] => 
                        )
    
                    [GeneralComments] => 1 SP Inductive
                    [AddressInfo] => SimpleXMLElement Object
                        (
                            [LocationTitle] => Sacramento County Public Garage
                            [AddressLine1] => 725 7th St
                            [AddressLine2] => SimpleXMLElement Object
                                (
                                )
    
                            [Town] => Sacramento
                            [StateOrProvince] => CA
                            [Postcode] => 95814
                            [Country] => United States
                            [Latitude] => 38.5846
                            [Longitude] => -121.4961
                            [ContactTelephone1] => 916-874-6227
                            [AccessComments] => 24 hours daily; pay lot
                            [RelatedURL] => SimpleXMLElement Object
                                (
                                )
    
                        )
    

    当我想获得SimpleXMLElement对象的一些值时,问题就出现了,例如,我想获得Town值,所以我按如下方式进行:

    $Town = $infos_point->ChargePoint[0]->AdressInfo->Town;
    print($Town);
    

    它给了我一张空白页。我在网上读到的东西都试过了,但还是一无所获。 也许有人能告诉我发生了什么?这将是伟大的,因为我将能够继续我的项目。

    1 回复  |  直到 12 年前
        1
  •  2
  •   juco    12 年前

    这是你对adDress的拼写。应该是: $Town = $infos_point->ChargePoint[0]->AddressInfo->Town;

    推荐文章