using Xml;

int main()
{
    var doc = Parser.parse_file ("didl.xml");
    var template = "DIDL-Item-%05d.xml";
    var count = 0;

    if (doc != null) {
        var new_doc = new Doc ();
        var root = doc->get_root_element ();
        var didl = root->copy (2);
        new_doc.set_root_element (didl);


        for (var node = root->children; node != null; node = node->next) {
            if (node->name == "item") {
                var item = node->copy(1);
                didl->add_child (item);
                string xml;
                new_doc.dump_memory_enc_format (out xml);

                var file = File.new_for_path (template.printf (count));
                count++;
                file.replace_contents (xml, xml.length, null, false, 0, null);

                item->unlink();
            }
        }

    }

    return 0;
}
