如何用xpath解析xml并检索元素数组?

时间:2014-02-02 10:30:00

标签: php xml arrays xpath

我正在努力让这件事情完成约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);
}

不起作用。

3 个答案:

答案 0 :(得分: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)
}

如果要处理多个元素,首先获取列表,迭代它并使用带有上下文节点的表达式。 context节点是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]=> ...

答案 1 :(得分:0)

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'];
                }
            }
        }
    }

    ?>

以下代码输出以下内容。这是一项繁忙的工作。我相信你可以重构它并让它动态地解析feed。

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

答案 2 :(得分:-1)

这是一个很糟糕的XML ......

以下是一些使用simplexmlxpath的代码。对不起,我对DOM的了解并不多。此示例需要PHP&gt; = 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&gt; = 5.4

更新获取@rel='extrabig'个节点的@href <pic:link>

$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;