【问题标题】:How to parse xml with xpath and retrieve an array of elements?如何使用 xpath 解析 xml 并检索元素数组?
【发布时间】:2014-02-02 10:29:59
【问题描述】:

我正在努力完成这件事大约 3 个小时,因此我将不胜感激。我需要解析下面的 xml 并提取“标题”、“描述”、“电话”、“地址”、“价格”、“图片”、“属性”。 我已经成功提取了标题、描述和电话。

我无法单独获取地址(位置)的纬度和经度。我得到 -1.3054552.7008 而不是纬度:-1.3054,经度:552.7008。

我不知道如何提取图片。它应该是一个数组。

我不知道如何提取属性。它应该是一个数组。

这是我的代码:

$adXml = file_get_contents('ad.xml');

$dom = new DOMDocument();
$dom->loadXml($adXml);
$xpath = new DOMXpath($dom);
$xpath->registerNamespace('ad', 'http://www.domainclassifiedsgroup.com/schema/ad/v1');
$xpath->registerNamespace('attribute', 'http://www.domainclassifiedsgroup.com/schema/attribute/v1');
$xpath->registerNamespace('pic', 'http://www.domainclassifiedsgroup.com/schema/picture/v1');
$xpath->registerNamespace('location', 'http://www.domainclassifiedsgroup.com/schema/location/v1');

#$ads = $xpath->evaluate('/ad:ad/ad:ad', NULL, FALSE);

$title = $xpath->evaluate('string(ad:title)', NULL, FALSE);
$description = $xpath->evaluate('string(ad:description)', NULL, FALSE);
$phone = $xpath->evaluate('string(ad:phone)', NULL, FALSE);

//the price is a kind of array so we ened to remove GBP
$price = $xpath->evaluate('string(ad:price)', NULL, FALSE);
$price = str_replace('GBP', '', $price);

//$pictures = $xpath->evaluate('string(pic:pictures)', NULL, FALSE);
$pictures = '?';
$attributes = '?';
$address = $xpath->evaluate('string(ad:ad-address)', NULL, FALSE);  // why is not array ?

xml在下面

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ad:ad xmlns:types="http://www.domainclassifiedsgroup.com/schema/types/v1" xmlns:cat="http://www.domainclassifiedsgroup.com/schema/category/v1" xmlns:loc="http://www.domainclassifiedsgroup.com/schema/location/v1" xmlns:ad="http://www.domainclassifiedsgroup.com/schema/ad/v1" xmlns:feat="http://www.domainclassifiedsgroup.com/schema/feature/v1" xmlns:attr="http://www.domainclassifiedsgroup.com/schema/attribute/v1" xmlns:pic="http://www.domainclassifiedsgroup.com/schema/picture/v1" xmlns:user="http://www.domainclassifiedsgroup.com/schema/user/v1" xmlns:rate="http://www.domainclassifiedsgroup.com/schema/rate/v1" xmlns:reply="http://www.domainclassifiedsgroup.com/schema/reply/v1" xmlns:feed="http://www.domainclassifiedsgroup.com/schema/feed/v1" xmlns:order="http://www.domainclassifiedsgroup.com/schema/order/v1" xmlns:payment="http://www.domainclassifiedsgroup.com/schema/payment/v1" locale="en_GB" version="1.17" id="1047060901">
  <ad:price>
    <types:currency-iso-code>
      <types:value localized-label="£">GBP</types:value>
    </types:currency-iso-code>
    <types:amount>17300</types:amount>
  </ad:price>
  <ad:title>BMW 330d M Sport Highline Coupe Black Diesel Auto + RED LEATHER</ad:title>
  <ad:description>2009 (59-reg). Sapphire black metallic. Coral red Dakota leather, electric adjustable seats with memory, heated seats, sport seats, USB interface for iPod, steering wheel gear shift, Angel daytime running lights, Xenon headlights, 19 inch Double spoke alloy wheels, fine brushed aluminium interior trim, power fold mirrors, M Sport Aerodynamic bodystyling package, loudspeaker audio upgrade, enhanced Bluetooth, climate control, cruise control, trip computer, ISOFIX child seat anchors, park sensors, split folding rear seats, remote locking alarm. FSH. 

Passenger Airbag; Air Conditioning; Premium Sound System; AM/FM Stereo; Xenon headlights; Climate Control; Alloy Wheels; Full Service History; Alarm; Tilt Steering Wheel; Central Locking; Anti-Lock Brakes (ABS); CD Player; Driver Airbag; Electric Windows; Power-assisted Steering (PAS); Electric heated seats; Safety Belt Pretensioners; Parking Sensors; V5 Registration Document; Cruise Control; Leather Seats; Immobiliser; Electric Adjustable Seats; Side Airbags; Rear seat belts; Electronic Stability Program (ESP) </ad:description>
  <ad:ad-address>
    <types:longitude>-1.30545</types:longitude>
    <types:latitude>52.7008</types:latitude>
  </ad:ad-address>
  <ad:search-distance>
    <ad:display-distance></ad:display-distance>
  </ad:search-distance>
  <ad:ad-status>
    <ad:value>ACTIVE</ad:value>
  </ad:ad-status>
  <ad:email>EXIST</ad:email>
  <ad:user-id>3463331380</ad:user-id>
  <ad:poster-ip-address/>
  <ad:phone>01530249618</ad:phone>
  <ad:modification-date-time>2014-02-02T03:02:41.000Z</ad:modification-date-time>
  <ad:start-date-time>2014-02-02T03:02:41.000Z</ad:start-date-time>
  <feat:features-active>
    <feat:feature-active display="true" name="AD_INSERTION" group="standard"/>
  </feat:features-active>
  <cat:category id="10303">
    <cat:id-name>bmw</cat:id-name>
    <cat:localized-name>BMW</cat:localized-name>
    <cat:categoriesTree/>
  </cat:category>
  <loc:locations>
    <loc:location id="10000393">
      <loc:id-name>england</loc:id-name>
      <loc:localized-name>England</loc:localized-name>
      <loc:parent-id/>
      <loc:location-breadcrumb/>
    </loc:location>
    <loc:location id="10000392">
      <loc:id-name>uk</loc:id-name>
      <loc:localized-name>United Kingdom</loc:localized-name>
      <loc:parent-id/>
      <loc:location-breadcrumb/>
    </loc:location>
    <loc:location id="11000025">
      <loc:id-name>leicestershire</loc:id-name>
      <loc:localized-name>Leicestershire</loc:localized-name>
      <loc:parent-id/>
      <loc:location-breadcrumb/>
    </loc:location>
  </loc:locations>
  <ad:neighborhood></ad:neighborhood>
  <attr:attributes>
    <attr:attribute localized-label="Fuel type" type="ENUM" name="vehicle_fuel_type">
      <attr:value localized-label="Diesel">diesel</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Year" type="INTEGER" name="vehicle_registration_year">
      <attr:value localized-label="2009">2009</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Body type" type="ENUM" name="vehicle_body_type">
      <attr:value localized-label="Coupe">coupe</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Model" type="STRING" name="vehicle_model">
      <attr:value>330</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Colour" type="STRING" name="vehicle_colour">
      <attr:value>Black</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Seller type" type="ENUM" name="seller_type">
      <attr:value localized-label="Trade">trade</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Mileage" type="LONG" name="vehicle_mileage">
      <attr:value>65000</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Transmission" type="ENUM" name="vehicle_transmission">
      <attr:value localized-label="Automatic">automatic</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Make" type="ENUM" name="vehicle_make">
      <attr:value localized-label="BMW">bmw</attr:value>
    </attr:attribute>
    <attr:attribute localized-label="Engine size" type="LONG" name="vehicle_engine_size">
      <attr:value>2993</attr:value>
    </attr:attribute>
  </attr:attributes>
  <ad:link rel="self" href="https://iphone-api.zmbtree.com/api/ads/1047060901"/>
  <ad:link rel="self-user" href="https://iphone-api.zmbtree.com/api/users/3463331380/ads/1047060901"/>
  <ad:link rel="self-public-website" href="http://www.zmbtree.com/p/cars-vans-motorbikes/bmw-330d-m-sport-highline-coupe-black-diesel-auto-red-leather/1047060901"/>
  <pic:pictures>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_77.JPG"/>
    </pic:picture>
    <pic:picture>
      <pic:link rel="extrabig" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_80.JPG"/>
      <pic:link rel="preview" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_81.JPG"/>
      <pic:link rel="big" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_79.JPG"/>
      <pic:link rel="thumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_78.JPG"/>
      <pic:link rel="moreadsthumb" href="http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_77.JPG"/>
    </pic:picture>
  </pic:pictures>
  <ad:visible-on-map>true</ad:visible-on-map>
  <ad:poster-contact-email>EXIST</ad:poster-contact-email>
  <ad:poster-contact-name/>
</ad:ad>

更新:尝试使用

检索图像
$pictures = $xml->xpath("//pic:pictures/pic:picture");

foreach ($pictures as $picture) {

  $pic = (string)$picture->xpath("pic:link/@extrabig")[0];

  print_r($pic);
}

没用。

【问题讨论】:

    标签: php xml arrays xpath


    【解决方案1】:

    如果您使用 string()、number() 或 boolean() 在 xpath 中转换元素,它将转换该元素的节点值。这意味着该元素及其后代中的所有文本内容。

    因此,如果您使用表达式string(ad:ad-address),它将返回两个子节点类型:经度和类型:纬度的内容。要将它们分开,您需要使用单独的表达式来获取经度/纬度值。

    $dom = new DOMDocument();
    $dom->loadXml($xml);
    $xpath = new DOMXpath($dom);
    $xpath->registerNamespace('ad', 'http://www.domainclassifiedsgroup.com/schema/ad/v1');
    $xpath->registerNamespace('types', 'http://www.domainclassifiedsgroup.com/schema/types/v1');
    
    var_dump(
      [
        'lng' => $xpath->evaluate('number(/ad:ad/ad:ad-address[1]/types:longitude)', NULL, FALSE),
        'lat' => $xpath->evaluate('number(/ad:ad/ad:ad-address[1]/types:latitude)', NULL, FALSE)
      ]
    );
    

    输出:

    array(2) {
      ["lng"]=>
      float(-1.30545)
      ["lat"]=>
      float(52.7008)
    }
    

    如果您想处理多个元素,您首先获取一个列表,对其进行迭代并使用带有上下文节点的表达式。上下文节点是 evaluate() 的第二个参数。如果你想从你迭代的元素中获取属性/内容,你可以使用 getAttribute() 或 $nodeValue 来读取它们。

    $dom = new DOMDocument();
    $dom->loadXml($xml);
    $xpath = new DOMXpath($dom);
    $xpath->registerNamespace('ad', 'http://www.domainclassifiedsgroup.com/schema/ad/v1');
    $xpath->registerNamespace('pic', 'http://www.domainclassifiedsgroup.com/schema/picture/v1');
    
    $result = [];
    $nodes = $xpath->evaluate('/ad:ad/pic:pictures/pic:picture', NULL, FALSE);
    foreach ($nodes as $node) {
      $row = [];
      foreach ($xpath->evaluate('pic:link', $node, FALSE) as $linkNode) {
        $row[$linkNode->getAttribute('rel')] = $linkNode->getAttribute('href'); 
      }
      $result[] = $row;
    }
    
    var_dump($result);
    

    输出:

    array(8) {
      [0]=>
      array(5) {
        ["extrabig"]=>
        string(102) "http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_80.JPG"
        ["preview"]=>
        string(102) "http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_81.JPG"
        ["big"]=>
        string(102) "http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_79.JPG"
        ["thumb"]=>
        string(102) "http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_78.JPG"
        ["moreadsthumb"]=>
        string(102) "http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_77.JPG"
      }
      [1]=> ...
    

    【讨论】:

      【解决方案2】:
      here is an alternative to xpath if you need it asap. 
      
      <?php
      
          $path = @file_get_contents('ad.xml');
          $xml = @simplexml_load_string($path);
          $namespaces = $xml->getNameSpaces(true);
      
          $ad = $xml->children($namespaces['ad']);
          $description = (string)$ad->description;
          echo $description;
      
          $title = (string)$ad->title;
          echo $title;
      
          $address = $ad->{'ad-address'}->children($namespaces['types']);
          echo $address->longitude;
          echo $address->latitude;
      
          $phone = (string)$ad->phone;
          echo $phone;
      
          $price = $ad->price->children($namespaces['types']);
          echo $price;
      
          $photos = $xml->children($namespaces['pic']);
          $pictures = $photos->children('pic');
      
          foreach($photos->pictures as $pictures)
          {
              foreach($pictures as $photo) 
              {
                  foreach ($photo->link as $link)
                  {
                      $att = $link->attributes();
                      if ($att['rel'] == 'extrabig')
                      {
                          echo $att['href'];
                      }
                  }
              }
          }
      
          ?>
      

      以下代码输出以下内容。这是一项匆忙的工作。我相信您可以重构它并使其动态解析提要。

      title: BMW 330d M Sport Highline Coupe Black Diesel Auto + RED LEATHER
      description: 2009 (59-reg). Sapphire black metallic. Coral red Dakota leather, electric adjustable seats with memory, heated seats, sport seats, USB interface for iPod, steering wheel gear shift, Angel daytime running lights, Xenon headlights, 19 inch Double spoke alloy wheels, fine brushed aluminium interior trim, power fold mirrors, M Sport Aerodynamic bodystyling package, loudspeaker audio upgrade, enhanced Bluetooth, climate control, cruise control, trip computer, ISOFIX child seat anchors, park sensors, split folding rear seats, remote locking alarm. FSH. Passenger Airbag; Air Conditioning; Premium Sound System; AM/FM Stereo; Xenon headlights; Climate Control; Alloy Wheels; Full Service History; Alarm; Tilt Steering Wheel; Central Locking; Anti-Lock Brakes (ABS); CD Player; Driver Airbag; Electric Windows; Power-assisted Steering (PAS); Electric heated seats; Safety Belt Pretensioners; Parking Sensors; V5 Registration Document; Cruise Control; Leather Seats; Immobiliser; Electric Adjustable Seats; Side Airbags; Rear seat belts; Electronic Stability Program (ESP) 
      latitude: -1.30545
      longitude: 52.7008
      phone: 01530249618
      price: 
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/C-QAAMXQWzNSeR-h/$(KGrHqJHJFEFJ0Ie)pzWBSeR-hK5zw~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/rwcAAOxyLN9SeR-h/$(KGrHqNHJEIFJbuUsB1BBSeR-hFuC!~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/DKgAAOxyzi9SeR-h/$T2eC16F,!yEFI9U4HtWeBSeR-h!PI!~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/p-QAAOxypNtSeR-h/$T2eC16VHJG8E9nyfnfl1BSeR-hFy)!~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/AhMAAOxy4t1SeR~d/$T2eC16h,!z!FJ4!MUtWSBSeR+d8SCw~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjkAAOxy3NBSeR~e/$(KGrHqN,!rkFJURnmyKpBSeR+eQ7Rg~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/x9YAAOxyXDhSeR~e/$(KGrHqFHJB8FJvCbFiKMBSeR+elk(Q~~48_80.JPG
      extrabig: http://i.domainimg.com/00/s/NTMxWDgwMA==/z/MjUAAOxy3NBSeR~d/$(KGrHqMOKpIFJ30nYMeqBSeR+e!FDg~~48_80.JPG
      

      【讨论】:

        【解决方案3】:

        这真是一个 XML 的地狱......

        这是一些代码,使用simplexmlxpath。抱歉,我不太喜欢DOM。此示例需要 PHP >= 5.4。

        $xml = simplexml_load_string($x); // assume XML in $x
        
        $title = (string)$xml->xpath("//ad:ad/ad:title")[0];
        $price = (string)$xml->xpath("//ad:ad/ad:price/types:amount")[0];
        $attributes = $xml->xpath("//attr:attributes/attr:attribute");
        
        echo $title . PHP_EOL;
        echo $price. PHP_EOL;
        
        foreach ($attributes as $a) {
            $label = (string)$a->xpath("@localized-label")[0];
            $value = (string)$a->xpath("./attr:value")[0];
            echo "$label: $value" . PHP_EOL;
        }
        

        您可以根据这些示例访问其他节点。

        看到它的工作:https://eval.in/97091

        评论:

        • 在不注册命名空间的情况下使用 xpath。
        • (string) = 将 xpath-results 转换为字符串而不是获取对象作为结果
        • ...)[0]; = 数组解引用,需要 PHP >= 5.4

        更新:获取&lt;pic:link&gt; 节点的@rel='extrabig' @href

        $pictures = $xml->xpath("//pic:pictures/pic:picture/pic:link[@rel = 'extrabig']/@href");
        $pictures = array_map('strval', $pictures); // converts every element to string
        foreach ($pictures as $pic) echo $pic . PHP_EOL;
        

        【讨论】:

        • 非常感谢!它看起来真的很棒!很遗憾有人投了反对票。我仍然在努力获取图像。我只对超大的感兴趣。这是我尝试过的 $pictures = $xml->xpath("//pic:pictures/pic:picture"); foreach ($pictures as $picture) { $pic = (string)$picture->xpath("pic:link/@extrabig")[0];打印_r($图片); }
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 1970-01-01
        • 2017-08-20
        相关资源
        最近更新 更多