2013-03-19 141 views
1

我正在写这篇文章,因为我试图解决一个似乎很笨的问题,但不幸的是我无法做到这一点。我有我与simplexml_load_file($link)如下解析XML文件(链接):从SimpleXMLElement对象获取值

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 = $infos_point->ChargePoint[0]->AdressInfo->Town; 
print($Town); 

它给了我一个空白页。我已经尝试了我在互联网上阅读的所有内容,但仍然没有任何结果 也许有人可以告诉我发生了什么?这将是伟大的,因为我将能够继续我的项目。

回答

2

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