Elliott
Elliott

Reputation: 3864

Catalyst XML datafeed

I'm trying to output the data from an XML file datafeed, the support documents are pretty poor and I'm having trouble looping through the XML.

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] => 0123456789 [fax] => 0123456789
                [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 
                        )
                    ) 
                )
            )
        )
    )

The data is loaded by:

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

I'm mainly interested in the vehicle data, which I have tried to load by:

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

Although this doesn't seem to work.

Be grateful if anybody could offer any advice :)

Thanks in advance.

Edit XML file (displaying only two vehicles for easy reading):

<?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>

Upvotes: 0

Views: 168

Answers (1)

kupson
kupson

Reputation: 7228

Use XPath to search for specific node data.

$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");
}

Upvotes: 1

Related Questions