2012-02-20 51 views
0

我試圖從XML文件datafeed輸出數據,支持文件非常差,我無法通過XML循環。Catalyst XML datafeed

$request = 
    "<"."?xml version='1.0' encoding='iso-8859-1'?".">". 
     "<download account='XXXX' password='XXXX' version='6' request='EXP' format='XML' dealer='XXXX' vehicles='Y' />"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml", "Content-Length: " . strlen($request))); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

curl_setopt($ch, CURLOPT_URL, 'https://www.catalyst-findit.co.uk/download.php'); 
$response = curl_exec($ch); 

$xml = simplexml_load_string($response); 
print_r($xml); 

我主要感興趣的車輛數據,我試圖加載由:

echo (string) $xml->vehicles->vehicle[0]; 

雖然這並不

SimpleXMLElement Object ( 
    [@attributes] => Array (
     [version] => 6 [type] => WEB [date] => 20120220) 
     [account] => SimpleXMLElement Object ( 
     [@attributes] => Array (
      [code] => XXXX 
     ) 
     [sites] => SimpleXMLElement Object ( 
      [site] => SimpleXMLElement Object ( 
       [@attributes] => Array ( 
        [code] => XXXX 
       ) 
       [name] => Name [address1] => Address 1 [address2] => town [address3] => UK [county] => County [postcode] => Postcode [phone] =>[fax] =>
       [vehicles] => SimpleXMLElement Object ( 
        [vehicle] => Array ( 
         [0] => SimpleXMLElement Object ( 
          [@attributes] => Array ( 
           [code] => 5956 [new] => N 
           [engineSize] => 998 
           [mileage] => 8000 
           [type] => MC 
           [reg] => XXX 
           [regDate] => 20070414 
           [created] => 20110928090130 
           [modified] => 20110928090130 
          ) [manufacturer] => Honda 
          [description] => CBR 1000 RR-7 
          [colour] => Black 
          [body] => Motorcycle 
          [fuel] => Petrol 
          [status] => In Stock 
         ) 
         [1] => SimpleXMLElement Object ( 
          [@attributes] => Array (
           [code] => 5958 
           [new] => Y 
           [engineSize] => 125 
           [type] => MC 
           [created] => 20110930090254 
           [modified] => 20110930090254 
          ) 
          [manufacturer] => Honda 
          [description] => WW125EX2A 
          [colour] => BLACK(NHA35) 
          [body] => SCOOTER 
          [status] => In Stock 
          [category] => SCOOTER 
         ) 
        ) 
       ) 
      ) 
     ) 
    ) 

的數據是由加載似乎工作。

不勝感激,如果有人可以提供任何建議:)

在此先感謝。

編輯 XML文件(只顯示兩個用於方便閱讀輛):

<?xml version="1.0" encoding="iso-8859-1"?> 
<findit xmlns="http://www.catalyst-findit.co.uk/download" version="6" type="WEB" date="20120220"> 
<account code="XXX"> 
<sites> 
<site code="BRI"> 
<name>XXX</name> 
<address1>XXX</address1> 
<address2>XXX</address2> 
<address3>XXX</address3> 
<county>XXX</county> 
<postcode>XXX</postcode> 
<phone>01472 123456</phone> 
<fax>01472 123456</fax> 
<vehicles> 
<vehicle code="XXX" new="N" engineSize="998" mileage="8000" type="MC" reg="XXX" regDate="20070414" created="20110928090130" modified="20110928090130"> 
<manufacturer>Honda</manufacturer> 
<description>CBR 1000 RR-7</description> 
<colour>Black</colour> 
<body>Motorcycle</body> 
<fuel>Petrol</fuel> 
<status>In Stock</status> 
</vehicle> 
<vehicle code="XXX" new="Y" engineSize="125" type="MC" created="20110930090254" modified="20110930090254"> 
<manufacturer>Honda</manufacturer> 
<description>XXX</description> 
<colour>BLACK(NHA35)</colour> 
<body>SCOOTER</body> 
<status>In Stock</status> 
<category>SCOOTER</category> 
</vehicle> 
</vehicles> 
</site> 
</sites> 
</account> 
</findit> 
+0

什麼是錯誤?請嘗試'curl_error'得到錯誤 – maxjackie 2012-02-20 12:43:26

+0

沒有顯示錯誤,'print_r'顯示上面的數據,我只是無法訪問車輛數據。 – Elliott 2012-02-20 12:47:33

回答

1

使用XPath搜索特定節點的數據。

$text = <<<EOF 
... XML data ... 
EOF; 

$xml = new SimpleXMLElement($text); 
# register xpath prefix for default namespace 
$namespaces = $xml->getDocNamespaces(); 
$xml->registerXPathNamespace('__ns', $namespaces['']); 
# get first vehicle data 
$result = $xml->xpath('//__ns:vehicles/__ns:vehicle[1]'); 
# get all vehicles in array 
$vehicles = $xml->xpath('//__ns:vehicles/__ns:vehicle'); 

print_r($result); # or 
echo($result[0]->colour); 

foreach ($vehicles as $v) { 
    echo ($v->colour . "\n"); 
} 
+0

我編輯了我的問題以提供XML文件。謝謝。 – Elliott 2012-02-20 13:41:45

+0

而我測試並修復(它必須使用命名空間)示例代碼:) – kupson 2012-02-20 14:30:42

+0

謝謝!這很好,如果可能的話,你可以在一個循環內展示它,以便不必指定車輛號碼? – Elliott 2012-02-20 14:42:35