Simple script to sort
It can be useful, if you pasted some items from other
Online version:
Offline version:
Put this code in .php file, set path to
items.xml
file by id
/fromid
attribute.It can be useful, if you pasted some items from other
items.xml
file.Online version:
Offline version:
PHP:
$path_to_file = 'items.xml';
$dom = new DOMDocument();
$dom->loadXML(file_get_contents($path_to_file), LIBXML_NOBLANKS);
$dom->formatOutput = true;
$items = $dom->getElementsByTagName('item');
$sorted = iterator_to_array($items);
usort($sorted, function ($a, $b) {
$aValue = 0;
if ($a->hasAttribute('id')) {
$aValue = $a->getAttribute('id');
}
if ($a->hasAttribute('fromid')) {
$aValue = $a->getAttribute('fromid');
}
$bValue = 0;
if ($b->hasAttribute('id')) {
$bValue = $b->getAttribute('id');
}
if ($b->hasAttribute('fromid')) {
$bValue = $b->getAttribute('fromid');
}
return $aValue - $bValue;
});
foreach ($sorted as $node) {
$items->item(0)->parentNode->appendChild($node);
}
file_put_contents('sorted_items.xml', str_replace('"/>', '" />', str_replace(' ', "\t", $dom->saveXML())));
items.xml
file and execute on command line php
Last edited: