遍历从Google Maps REST API收到的带有非唯一元素名称的simplexml_load_file对象

时间:2023-02-04 20:19:03

I am trying to get the value inside the <address_component> element that corresponds to <type>postal_code</type> from the XML returned by the Google maps REST API.

我试图从谷歌地图REST API返回的XML中获取与 postal_code 对应的 元素内的值。

For example, in the below:

例如,在下面:

    ...

  <result>    
    <type>
      route
    </type>    
    <formatted_address>
      Rose Ln, Liverpool, Merseyside L18 5ED, UK
    </formatted_address>    
    <address_component>      
      <long_name>
        Rose Ln
      </long_name>      
      <short_name>
        Rose Ln
      </short_name>      
      <type>
        route
      </type>      
    </address_component>    
    <address_component>      
      <long_name>
        Liverpool
      </long_name>      
      <short_name>
        Liverpool
      </short_name>      
      <type>
        locality
      </type>      
      <type>
        political
      </type>      
    </address_component>    
    <address_component>      
     ...      
    </address_component>    
    <address_component>      
    ...
    </address_component>    
    <address_component>      
      <long_name>
        L18 5ED
      </long_name>      
      <short_name>
        L18 5ED
      </short_name>      
      <type>
        postal_code
      </type>      
    </address_component>    
    <address_component>      
    ...      
    </address_component>    
   ...    
  </result>

  ... more result elements

We see one of the <result> elements. Nested inside are multiple <address_component> elements. Inside one of those is <type>postal_code</type>.

我们看到了 元素之一。嵌套在里面的是多个 元素。其中一个是 postal_code 。

My question is how would I distinguish between these same named elements and choose only the <address_component> with <type>postal_code</type> attributed to it?

我的问题是如何区分这些相同的命名元素,并仅选择 postal_code ?

If they were uniquely named it would be a simple case of:

如果它们具有唯一名称,那将是一个简单的例子:

foreach ($address_rsponse->result as $address_option) {
    $val = $address_option->some_unique_name;      
}

However when named the same I am stumped as to a method to pick out an element by it's child-element.

然而,当命名相同时,我感到困惑的是通过它的子元素挑选元素的方法。

Could anyone shed any light on the correct approach.

任何人都可以对正确的方法有所了解。

Thanks

谢谢

1 个解决方案

#1


1  

You can use XPath with SimpleXML (docs).

您可以将XPath与SimpleXML(docs)一起使用。

The following will give you an array containing the <address_component> element(s) that you're looking for.

以下将为您提供一个包含您正在寻找的 元素的数组。

$address_rsponse->result->xpath(
    'address_component[normalize-space(type)="postal_code"]')

If you want to loop over the <result> elements, the following will give you an array containing the <address_component> element(s) that you're looking for and output the first for each <result>.

如果要循环遍历 元素,以下内容将为您提供一个包含您正在查找的 元素的数组,并为每个 输出第一个元素。

foreach ($address_rsponse->result as $result) {
    $postal_codes = $result->xpath('address_component[normalize-space(type)="postal_code"]');
    // Do whatever with the postal code(s)
    echo trim($postal_codes[0]->long_name);
}

#1


1  

You can use XPath with SimpleXML (docs).

您可以将XPath与SimpleXML(docs)一起使用。

The following will give you an array containing the <address_component> element(s) that you're looking for.

以下将为您提供一个包含您正在寻找的 元素的数组。

$address_rsponse->result->xpath(
    'address_component[normalize-space(type)="postal_code"]')

If you want to loop over the <result> elements, the following will give you an array containing the <address_component> element(s) that you're looking for and output the first for each <result>.

如果要循环遍历 元素,以下内容将为您提供一个包含您正在查找的 元素的数组,并为每个 输出第一个元素。

foreach ($address_rsponse->result as $result) {
    $postal_codes = $result->xpath('address_component[normalize-space(type)="postal_code"]');
    // Do whatever with the postal code(s)
    echo trim($postal_codes[0]->long_name);
}