PHP XML sorting

Possible duplicate:
SimpleXMLElement object

Hi, I want to sort an XML document - I do it like this:

public function xsort(&$nodes, $child_name, $order=SORT_ASC)
{
    $sort_proxy = array();

    foreach($nodes as $k => $node) {
        $sort_proxy[$k] = (string) $node->$child_name;
    }

    array_multisort($sort_proxy, $order, $nodes);

}

And I get a sorted array in a variable $nodes. In the code, it is used as follows:

$this->xsort($nodes, 'kategoria_6', SORT_DESC);

When I do print_r ($ nodes); I get a sorted view

SimpleXMLElement Object (
  [kategoria_6] => kat4 
  [opis_6] => opis4 
  [img] => slides/slide3.jpg
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat3 
  [opis_6] => opis3 
  [img] => slides/slide2.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat2 
  [opis_6] => opis2 
  [img] => slides/slide1.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat1 
  [opis_6] => opis1 
  [img] => slides/slide0.jpg 
) 
SimpleXMLElement Object ( 
  [kategoria_6] => kat1 
  [opis_6] => opis1 
  [img] => slides/slide4.jpg 
)

But when I want to save it to a file, it will be unsorted. File save code:

$dom = new DOMDocument('1.0', 'utf-8');
foreach ($nodes as $element) {
    $dom = dom_import_simplexml($element)->ownerDocument;
}
$dom->save("c:\\aaa.xml");

Does anyone know why it saves unsorted data?

+3
source share

All Articles