Skip to content

Instantly share code, notes, and snippets.

@cstrouse
Created November 10, 2015 04:53
Show Gist options
  • Select an option

  • Save cstrouse/28108dd2e6f3151dc08e to your computer and use it in GitHub Desktop.

Select an option

Save cstrouse/28108dd2e6f3151dc08e to your computer and use it in GitHub Desktop.
<?php
// fetches a file and returns an XML document
$xml = simplexml_load_file("http://search.cdsonline.com.au/offsite/xmlStock.asp?dlr=wesmotperf");
// loads the XML into the DOM
$dom1 = new DomDocument();
$dom1->loadXML($xml->asXML());
// Sets up an object so we can use xpath to query for nodes of the XML document
$xpath = new domXPath($dom1);
// Queries for <stocklist><vehicle>, each vehicle is wrapped in a vehicle tag
$xpathQuery = $xpath->query('/stocklist/vehicle');
// Sets the MIME type for the data we're outputting so the browser knows how to handle it
header('Content-type: application/xml');
// Send the opening root tag; this contains all of the "records" essentially
echo "<stocklist>";
// Loop through the vehicle nodes
foreach($xpathQuery as $vehicle) {
// Fetch the stock number for the vehicle
$stk_number = $vehicle->getElementsByTagName('stk');
// Fetch the number of image tags that are children of the vehicle node
$image_count = $vehicle->getElementsByTagName('image')->length;
// Send an opening vehicle tag
echo "<vehicle>";
// Send the stock number
echo "<stk>" . $stk_number->item(0)->nodeValue . "</stk>";
// Send the photo count
echo "<photos>$image_count</photos>";
// Send the closing vehicle tag
echo "</vehicle>";
}
// Send the closing stocklist tag
echo "</stocklist>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment