note 89330 deleted from function.domxml-open-file by sobak
| From: | sobak@php.net | Date: | Tue, 22 Nov 2016 03:45:00 +0000 |
| Subject: | note 89330 deleted from function.domxml-open-file by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207436@lists.php.net to get a copy of this message | ||
Note Submitter: koms at koms dot ru
----
The way to order the nodes in XML-files by an attribute (desc or asc):
<?php
// open the XML-file
$xml = domxml_open_file("our_file.xml");
// get the root element
$root = $xml->document_element();
// get the list of the nodes
$nodes = $root->child_nodes();
// create the array 'messages' of the nodes
foreach($nodes as $node)
{
if ($node->node_name() == 'photo')
{
$currentMessage['thumbnail'] = $node->get_attribute('thumbnail');
$currentMessage['filename'] = $node->get_attribute('filename');
$currentMessage['idnum'] = $picnum[md5($node->get_attribute('filename'))];
$messages[] = $currentMessage;
}
}
// sort all the nodes by idnum (asc/desc is "1: -1" or "-1: 1")
$compare_func = create_function('$a, $b', 'return
($a[\'idnum\']==$b[\'idnum\'])? 0:
(($a[\'idnum\']>$b[\'idnum\'])? 1: -1);');
usort($messages, $compare_func);
// create the new XML-data
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("photos");
$root = $doc->append_child($root);
for($ee=0;$ee<count($messages);$ee++)
{
$msgNode = $root->new_child('photo');
foreach($messages[$ee] as $description => $result)
{
$msgNode->set_attribute($description,$result);
}
}
// write down the XML-file
$text = $doc->dump_mem(true);
$fp = fopen("our_file.xml","w");
fwrite($fp,$text);
fclose($fp);
?>