XML

The Cloud CMS Bulk Import tool makes it easy to import content from an XML file into a Cloud CMS content repository. XML is a common format for content that has been exported from an existing database or legacy CMS system.

Suppose you have a XML data set containing information about soccer leagues around the world. It might look something like this:

<?xml version='1.0' encoding='utf-8'?>
<root>
    <leagues>
        <league code="bol" name="Nacional" country="Bolivia"/>
        <league code="eng" name="Barclays Premier League" country="England"/>
        <league code="usa" name="MLS" country="USA"/>
    </leagues>
</root>

The snippet above is truncated to just show three leagues (for brevity) and is by no means a mindfully curated list. The actual list would have many more entries but, for now, these will do.

Suppose this XML file is located at data/soccer-leagues.xml.

We might also define a my:league content type definition that looks like this:

{
    "title": "League",
    "type": "object",
    "properties": {
        "title": {
            "title": "Title",
            "type": "string"
        },
        "code": {
            "title": "Code",
            "type": "string"
        },
        "country": {
            "title": "Country",
            "type": "string"
        }
    },
    "_qname": "my:league",
    "_type": "d:type",
    "_parent": "n:node"
}

This content type definition could be located at types/my_league/node.json.

We can import this content into Cloud CMS by using the cloudcms-packager module like this:

var fs = require("fs");
var parseString = require("xml2js").parseString;

// create a packager
var PackagerFactory = require("cloudcms-packager");
PackagerFactory.create(function(err, packager) {

    // package up the "my:league" content type definition
    packager.addFromDisk("./types/my_league/node.json");

    // parse XML and create content instances
    var xmlText = fs.readFileSync("./data/soccer-leagues.xml").toString();
    parseString(xmlText, function (err, result) {

        var array = result.root.leagues[0].league;
        for (var i = 0; i < array.length; i++)
        {
            var entry = JSON.parse(JSON.stringify(array[i]["$"]));
            entry._type = "my:league";
            entry.title = entry.name;
            delete entry.name;

            packager.addNode(entry);
        }

        // commit
        packager.package(function(err, archiveInfo) {
            console.log("Wrote archive: " + archiveInfo.filename);
        });
    });
});

For a full example of this code in action, please see:

https://github.com/gitana/cloudcms-packager/tree/master/examples/xml/soccer-leagues/example1