note 89330 added to function.domxml-open-file

From: Date: Tue, 03 Mar 2009 19:18:50 +0000
Subject: note 89330 added to function.domxml-open-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-151414@lists.php.net to get a copy of this message
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); ?> ---- Server IP: 78.41.104.32 Probable Submitter: 212.42.51.84 ---- Manual Page -- http://www.php.net/manual/en/function.domxml-open-file.php Edit -- https://master.php.net/note/edit/89330 Del: integrated -- https://master.php.net/note/delete/89330/integrated Del: useless -- https://master.php.net/note/delete/89330/useless Del: bad code -- https://master.php.net/note/delete/89330/bad+code Del: spam -- https://master.php.net/note/delete/89330/spam Del: non-english -- https://master.php.net/note/delete/89330/non-english Del: in docs -- https://master.php.net/note/delete/89330/in+docs Del: other reasons-- https://master.php.net/note/delete/89330 Reject -- https://master.php.net/note/reject/89330 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#151414) next »