简体   繁体   English

SimpleXML用PHP选择属性?

[英]SimpleXML selecting attributes with PHP?

Okay so I've never worked with SimpleXML before and I'm having a few problems 好的,所以我以前从未使用过SimpleXML,并且遇到了一些问题

Here's my PHP: 这是我的PHP:

map.api.php map.api.php

$location = $_SESSION['location'];
$address = $location; // Business Location
$prepAddr = str_replace(' ','+',$address);
$feedUrl="http://nominatim.openstreetmap.org/search?q=". $prepAddr ."&format=xml&addressdetails=1&polygon=1";

$sxml = simplexml_load_file($feedUrl);

foreach($sxml->attributes() as $type){
  $lat = $type->place['lat'];
  $long = $type->place['lon'];
}

And here's an example of the XML table I'm working from. 这是我正在使用的XML表的示例。

<searchresults timestamp="Thu, 28 Jan 16 14:16:34 +0000" attribution="Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright" querystring="M27 6bu, 149 Station road, Swinton, Manchester" polygon="true" exclude_place_ids="65827001" more_url="http://nominatim.openstreetmap.org/search.php?format=xml&exclude_place_ids=65827001&accept-language=en-US,en;q=0.8&polygon=1&addressdetails=1&q=M27+6bu%2C+149+Station+road%2C+Swinton%2C+Manchester">
  <place place_id="65827001" osm_type="way" osm_id="32861649" place_rank="26" boundingbox="53.5122168,53.5190893,-2.3402445,-2.3331231" lat="53.5156919" lon="-2.3368185" display_name="Station Road, Newtown, Salford, Greater Manchester, North West England, England, M27 4AE, United Kingdom" class="highway" type="secondary" importance="0.5">
    <road>Station Road</road>
    <suburb>Newtown</suburb>
    <town>Salford</town>
    <county>Greater Manchester</county>
    <state_district>North West England</state_district>
    <state>England</state>
    <postcode>M27 4AE</postcode>
    <country>United Kingdom</country>
    <country_code>gb</country_code>
  </place>
</searchresults>

I want to select the " lat " and " lon " attributes from < place >, but when I echo $lat and $long they are empty, why? 我想从< place >中选择“ lat ”和“ lon ”属性,但是当我回显$lat$long它们为空,为什么呢?

When you call attributes as you did above, it's only going to act on the first element, which in your case is the root. 当您像上面一样调用属性时,它只会作用于第一个元素,在您的情况下,它是根。 You need to call attributes on the element you want the attributes for. 您需要在需要属性的元素上调用属性。 Easiest way to do that would be 最简单的方法是

$sxml->place[0]->attributes()

Does that make sense? 那有意义吗? Basically you're telling SimpleXML to seek to the element you want to analyze, then returning a new SimpleXML object that represents that element's attributes. 基本上,您是在告诉SimpleXML查找要分析的元素,然后返回一个表示该元素属性的新SimpleXML对象。 See if the docs help 查看文件是否有帮助

Another option you have is using xpath to return all place elements, then iterating through those and calling attributes() on each element, in the case where you have more than one place. 您拥有的另一种选择是使用xpath返回所有place元素,然后迭代这些元素并在每个元素上调用attribute(),以防您拥有多个地方。

Okay so I fixed this myself. 好吧,我自己解决了这个问题。

Instead of running everything through the foreach loop (as shown below): 而不是通过foreach循环运行所有内容(如下所示):

foreach($sxml->attributes() as $type){
 $lat = $type->place['lat'];
 $long = $type->place['lon'];
}

I just directly got the attribute values and stored them in a variable: 我只是直接获得属性值并将它们存储在变量中:

$lat = $sxml->place->attributes()->lat;
$long = $sxml->place->attributes()->lon;

This then returned an error/warning: Warning: main() [function.main]: Node no longer exists 然后返回错误/警告: Warning: main() [function.main]: Node no longer exists

By using isset and checking if the value exists first, you can get around this. 通过使用isset并检查该值是否首先存在,可以解决此问题。

if (isset($sxml->place))
{
 $lat = $sxml->place->attributes()->lat;
 $long = $sxml->place->attributes()->lon;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM