1

我写那篇文章是因为我试图解决一个看起来很愚蠢的问题,但不幸的是我做不到。我有一个正在解析的 XML 文件(链接),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);

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

4

1 回答 1

2

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

于 2013-03-19T21:45:54.893 回答