MWZ's picture

With the following report you can see, how easy it is to export ARIS models as an xml file.

This simple report exports the selected models with their object definitions and cxns to an xml file.

(Remark: This report doesn’t handle multiple occurrences of the same object definition in the model.)

To create the xml output object you use the following method:

var xmlOutput = Context.createXMLOutputObject(Context.getSelectedFile(), "Root");

The XML elements that can then be used in the script are defined in org.jdom.*

In this example the different items are written with the function “writeXmlItem”. This function creates a new xml element per item and adds the attributes “Name”, and “Identifier” and the type number as xml attributes.

function writeXmlItem(xmlOutput, xmlParent, oItem, sElement) {
    var xmlItem = xmlOutput.addElement(xmlParent, sElement);
    xmlItem.setAttribute("Name", oItem.Name(g_nLoc));
    xmlItem.setAttribute("TypeNum", oItem.TypeNum());
    xmlItem.setAttribute("ID", getAttrID(oItem));
    return xmlItem;
}

The function “writeXmlCxn” is used to write the connection elements with name of source and target object, the type number and the attribute “Identifier”.

function writeXmlCxn(xmlOutput, xmlParent, oCxn, sElement) {
    var xmlCxn = xmlOutput.addElement(xmlParent, sElement);
    xmlCxn.setAttribute("SourceName", oCxn.SourceObjDef().Name(g_nLoc));
    xmlCxn.setAttribute("TargetName", oCxn.TargetObjDef().Name(g_nLoc));    
    xmlCxn.setAttribute("TypeNum", oCxn.TypeNum());
    xmlCxn.setAttribute("ID", getAttrID(oCxn));
    return xmlCxn;
}

The above used function “getAttrID” to get value of attribute “Identifier” looks like this:

function getAttrID(oItem) {
    return oItem.Attribute(Constants.AT_ID, g_nLoc).getValue();
}

So that the whole script looks as follows:

var g_nLoc = Context.getSelectedLanguage(); 
var xmlOutput = Context.createXMLOutputObject(Context.getSelectedFile(), "Root");

var oSelModels = ArisData.getSelectedModels();
for (var i = 0; i < oSelModels.length; i++) {
    var oModel = oSelModels[i];
    var xmlModel = writeXmlItem(xmlOutput, xmlOutput.getRootElement(), oModel, "Model");
    
    var oObjDefs = oModel.ObjDefList();
    for (var j = 0; j < oObjDefs.length; j++) {
        var oObjDef = oObjDefs[j];
        var xmlObjDef = writeXmlItem(xmlOutput, xmlModel, oObjDef, "ObjDef");
    }
    
    var oCxnDefs = oModel.CxnList();
    for (var j = 0; j < oCxnDefs.length; j++) {
        var oCxnDef =  oCxnDefs[j]
        var xmlCxnDef = writeXmlCxn(xmlOutput, xmlModel, oCxnDef, "CxnDef");
    }
}
xmlOutput.WriteReport();

/****************************************************************************/

function writeXmlItem(xmlOutput, xmlParent, oItem, sElement) {
    var xmlItem = xmlOutput.addElement(xmlParent, sElement);
    xmlItem.setAttribute("Name", oItem.Name(g_nLoc));
    xmlItem.setAttribute("TypeNum", oItem.TypeNum());
    xmlItem.setAttribute("ID", getAttrID(oItem));
    return xmlItem;
}

function writeXmlCxn(xmlOutput, xmlParent, oCxn, sElement) {
    var xmlCxn = xmlOutput.addElement(xmlParent, sElement);
    xmlCxn.setAttribute("SourceName", oCxn.SourceObjDef().Name(g_nLoc));
    xmlCxn.setAttribute("TargetName", oCxn.TargetObjDef().Name(g_nLoc));    
    xmlCxn.setAttribute("TypeNum", oCxn.TypeNum());
    xmlCxn.setAttribute("ID", getAttrID(oCxn));
    return xmlCxn;
}

function getAttrID(oItem) {
    return oItem.Attribute(Constants.AT_ID, g_nLoc).getValue();
}

Here you can download the entire source code from the "XML Export" report.

Note: This article describe how to develop a report in ARIS. See this post for links to similar articles.

Tags: script export ARIS