Page 1 of 1

Sorting an XML list...

Posted: Fri Sep 02, 2011 4:00 pm
by Bardo
Help! I'm still pretty new at this php malarky so please be gentle...

Basically what I'm trying to do is to load an XML file structured like so...

Code: Select all

<?xml version="1.0"?>
<training>
	<course>
		<startdate>2011-29-02</startdate>
		<displaydate>29th February - 5th March</displaydate>
		<name>CCR Normoxic</name>
		<location>Malta</location>
	</course>
	<course>
		<startdate>2011-29-04</startdate>
		<displaydate>5th - 12th April</displaydate>
		<name>CCR Normoxic</name>
		<location>Malta</location>
	</course>
	<course>
		<startdate>2011-29-03</startdate>
		<displaydate>14th - 21st March</displaydate>
		<name>Rebreather</name>
		<location>Gran Canaria</location>
	</course>
	<course>
		<startdate>2011-29-07</startdate>
		<displaydate>18th - 25th July</displaydate>
		<name>CCR Hypoxic</name>
		<location>Malta</location>
	</course>
</training>
I'm loading the XML file using the following method...

Code: Select all

$training = new SimpleXMLElement('training.xml', null, true);
..and then writing the XML file back out to the server using the following...

Code: Select all

$training->asXML('training.xml');
Basically what I need to do is to sort the XML list using the 'start date' element. I've spend a good few hours googling various php code solutions but none of them seem to work. Anyone got a very simple solution that will crack this?! Help!!!!!

Re: Sorting an XML list...

Posted: Sun Sep 04, 2011 12:14 am
by 2-d
Hey there,

I think the easiest way to do this would be to parse out your XML. Then convert the 'startdate' to a unix time stamp using the php function strtotime. Then you can just sort on that unix time stamp.

Once thats done, just start re building the XML putting the elements back in after they are sorted.

Best of luck!

Re: Sorting an XML list...

Posted: Sun Sep 04, 2011 3:31 pm
by ok
I think the simplest way to do this is to convert the XML structure into a 2-dim array with keys set as the startdate field, and then use ksort on that array. After that the only thing left is to convert the array back to XML file.

Re: Sorting an XML list...

Posted: Mon Sep 05, 2011 6:06 pm
by jonesi
Here is my solution. It's not very elegant but it works!

Code: Select all

<?php
$myXml = '
<training>
        <course>
                <startdate>2011-29-02</startdate>
                <displaydate>29th February - 5th March</displaydate>
                <name>CCR Normoxic</name>
                <location>Malta</location>
        </course>
        <course>
                <startdate>2011-29-04</startdate>
                <displaydate>5th - 12th April</displaydate>
                <name>CCR Normoxic</name>
                <location>Malta</location>
        </course>
        <course>
                <startdate>2011-29-03</startdate>
                <displaydate>14th - 21st March</displaydate>
                <name>Rebreather</name>
                <location>Gran Canaria</location>
        </course>
        <course>
                <startdate>2011-29-07</startdate>
                <displaydate>18th - 25th July</displaydate>
                <name>CCR Hypoxic</name>
                <location>Malta</location>
        </course>
</training>';

// Create simple xml object and print it out.
$xmlObj = simplexml_load_string($myXml);
var_dump($xmlObj);

echo '<BR>';

// Now create two arrays which we will later combine.
$dateArray = array(); // This one is of type date/time which will be the key of a new combined array. 
$courseXMLstringAray = array(); // This will hold each <course> element as a string.

// Now loop through the <course> elements and populate our arrays.
foreach ($xmlObj->course as $course) {
	// Get the date string.
	$startDate = $course->startdate;
	// Convert it to a date/time type (so we can sort it).  
	$date = mktime(0,0,0,substr($startDate,8,2),substr($startDate,5,2),substr($startDate,0,4));
	// Add It to the new array.
	array_push($dateArray, $date);
	array_push($courseXMLstringAray, $course->asXML());	
}
// Use the dateArray to index the array of <course> elements (in string format).
$courseXMLstringAray = array_combine($dateArray, $courseXMLstringAray); 
// Now we have a new indexed array we can sort it!!
ksort($courseXMLstringAray);
// Now we can reconstruct a new XML document in the correct order.
$newXMLStr = '';
foreach ($courseXMLstringAray as $courseXMLstr) {
	$newXMLStr = $newXMLStr . $courseXMLstr;
}
$newXMLStr = '<training>' . $newXMLStr . '</training>';
$xmlObj = simplexml_load_string($newXMLStr);
// Now we are back where we started but in the correct order!
var_dump($xmlObj);
?>
Oh, and by the way there is no 29th of Feb this year!

Re: Sorting an XML list...

Posted: Mon Sep 05, 2011 6:47 pm
by Bardo
Thanks to everyone who has replied so far! I'd especially like to thank Jonesi - you are an absolute star! I just tried your code and it worked perfectly :-)

Re: Sorting an XML list...

Posted: Tue Sep 06, 2011 11:39 am
by jonesi
You're welcome.