• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

items.xml sorter - by id/fromid

Gesior.pl

Mega Noob&LOL 2012
Senator
Joined
Sep 18, 2007
Messages
2,955
Solutions
98
Reaction score
3,351
Location
Poland
GitHub
gesior
Simple script to sort 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())));
Put this code in .php file, set path to items.xml file and execute on command line php
 
Last edited:
Sorry for golden shovel :D but i'm used this script and it's made something wrong like
XML:
<item id="1487" article="a" name="fire field" >
<attribute key="type" value="magicfield" />
<attribute key="decayTo" value="1488" />
<attribute key="duration" value="120" />
<attribute key="field" value="fire" />
</item>
and oryginal items with id 1487
XML:
    <item id="1487" article="a" name="fire field">
        <attribute key="type" value="magicfield" />
        <attribute key="decayTo" value="1488" />
        <attribute key="duration" value="120" />
        <attribute key="field" value="fire">
        <attribute key="damage" value="20" />
        <attribute key="ticks" value="10000" />
        <attribute key="count" value="7" />
    </item>
 
Sorry for golden shovel :D but i'm used this script and it's made something wrong like
XML:
<item id="1487" article="a" name="fire field" >
<attribute key="type" value="magicfield" />
<attribute key="decayTo" value="1488" />
<attribute key="duration" value="120" />
<attribute key="field" value="fire" />
</item>
and oryginal items with id 1487
XML:
    <item id="1487" article="a" name="fire field">
        <attribute key="type" value="magicfield" />
        <attribute key="decayTo" value="1488" />
        <attribute key="duration" value="120" />
        <attribute key="field" value="fire">
        <attribute key="damage" value="20" />
        <attribute key="ticks" value="10000" />
        <attribute key="count" value="7" />
    </item>
1. Your code is not valid XML code.
Code:
<item id="1487" article="a" name="fire field">
        <attribute key="type" value="magicfield" />
        <attribute key="decayTo" value="1488" />
        <attribute key="duration" value="120" />
        <attribute key="field" value="fire">               /// <<<< IT'S XML OPENING TAG (does not have /> at end)
        <attribute key="damage" value="20" />
        <attribute key="ticks" value="10000" />
        <attribute key="count" value="7" />

</attribute>                                                          /// <<<< IT SHOULD BE CLOSED AFTER ALL NESTED 'attribute's

    </item>
2. My code did not work with nested 'attributes', BUT I've updated it. Copy new code from first post.
3. I made online version: OTS.ME - items.xml sorter - sorts by "id" and "fromid" (https://ots.me/items-xml-sorter/)
 
Last edited:
Back
Top