XML codec for JavaScript object graphs. See mxObjectCodec for a description of the general encoding/decoding scheme. This class uses the codecs registered in mxCodecRegistry for encoding/decoding each object.
In order to resolve references, especially forward references, the mxCodec constructor must be given the document that contains the referenced elements.
The following code is used to encode a graph model.
let encoder = new mxCodec(); let result = encoder.encode(graph.getModel()); let xml = mxUtils.getXml(result);
Using the code below, an XML document is decoded into an existing model. The document may be obtained using one of the functions in mxUtils for loading an XML file, eg. mxUtils.get, or using mxUtils.parseXml for parsing an XML string.
let doc = mxUtils.parseXml(xmlString); let codec = new mxCodec(doc); codec.decode(doc.documentElement, graph.getModel());
This example demonstrates parsing a list of isolated cells into an existing graph model. Note that the cells do not have a parent reference so they can be added anywhere in the cell hierarchy after parsing.
let xml = '<root><mxCell id="2" value="Hello," vertex="1"><mxGeometry x="20" y="20" width="80" height="30" as="geometry"/></mxCell><mxCell id="3" value="World!" vertex="1"><mxGeometry x="200" y="150" width="80" height="30" as="geometry"/></mxCell><mxCell id="4" value="" edge="1" source="2" target="3"><mxGeometry relative="1" as="geometry"/></mxCell></root>'; let doc = mxUtils.parseXml(xml); let codec = new mxCodec(doc); let elt = doc.documentElement.firstChild; let cells = []; while (elt != null) { cells.push(codec.decode(elt)); elt = elt.nextSibling; } graph.addCells(cells);
Using the following code, the selection cells of a graph are encoded and the output is displayed in a dialog box.
let enc = new mxCodec(); let cells = graph.getSelectionCells(); mxUtils.alert(mxUtils.getPrettyXml(enc.encode(cells)));
Newlines in the XML can be converted to <br>, in which case a ‘<br>’ argument must be passed to mxUtils.getXml as the second argument.
For debugging I/O you can use the following code to get the sequence of encoded objects:
let oldEncode = mxCodec.prototype.encode; mxCodec.prototype.encode = function(obj) { mxLog.show(); mxLog.debug('mxCodec.encode: obj='+mxUtils.getFunctionName(obj.constructor)); return oldEncode.apply(this, arguments); };
Note that the I/O system adds object codecs for new object automatically. For decoding those objects, the constructor should be written as follows:
let MyObj = function(name) { // ... };
mxCodec | XML codec for JavaScript object graphs. |
Functions | |
mxCodec | Constructs an XML encoder/decoder for the specified owner document. |
Variables | |
document | The owner document of the codec. |
objects | Maps from IDs to objects. |
elements | Lookup table for resolving IDs to elements. |
encodeDefaults | Specifies if default values should be encoded. |
Functions | |
putObject | Assoiates the given object with the given ID and returns the given object. |
getObject | Returns the decoded object for the element with the specified ID in document. |
lookup | Hook for subclassers to implement a custom lookup mechanism for cell IDs. |
getElementById | Returns the element with the given ID from document. |
updateElements | Returns the element with the given ID from document. |
addElement | Adds the given element to elements if it has an ID. |
getId | Returns the ID of the specified object. |
reference | Hook for subclassers to implement a custom method for retrieving IDs from objects. |
encode | Encodes the specified object and returns the resulting XML node. |
decode | Decodes the given XML node. |
encodeCell | Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitely used by the respective object encoders (eg. |
isCellCodec | Returns true if the given codec is a cell codec. |
decodeCell | Decodes cells that have been encoded using inversion, ie. |
insertIntoGraph | Inserts the given cell into its parent and terminal cells. |
setAttribute | Sets the attribute on the specified node to value. |
function mxCodec( document )
Constructs an XML encoder/decoder for the specified owner document.
document | Optional XML document that contains the data. If no document is specified then a new document is created using mxUtils.createXmlDocument. |
mxCodec.prototype.getElementById = function( id )
Returns the element with the given ID from document.
id | String that contains the ID. |
mxCodec.prototype.updateElements = function()
Returns the element with the given ID from document.
id | String that contains the ID. |
mxCodec.prototype.addElement = function( node )
Adds the given element to elements if it has an ID.
mxCodec.prototype.getId = function( obj )
Returns the ID of the specified object. This implementation calls reference first and if that returns null handles the object as an mxCell by returning their IDs using mxCell.getId. If no ID exists for the given cell, then an on-the-fly ID is generated using mxCellPath.create.
obj | Object to return the ID for. |
mxCodec.prototype.reference = function( obj )
Hook for subclassers to implement a custom method for retrieving IDs from objects. This implementation always returns null.
let codec = new mxCodec(); codec.reference = function(obj) { return obj.getCustomId(); };
obj | Object whose ID should be returned. |
mxCodec.prototype.decode = function( node, into )
Decodes the given XML node. The optional “into” argument specifies an existing object to be used. If no object is given, then a new instance is created using the constructor from the codec.
The function returns the passed in object or the new instance if no object was given.
node | XML node to be decoded. |
into | Optional object to be decodec into. |
mxCodec.prototype.encodeCell = function( cell, node, includeChildren )
Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitely used by the respective object encoders (eg. mxModelCodec, mxChildChangeCodec and mxRootChangeCodec). This implementation writes the given cell and its children as a (flat) sequence into the given node. The children are not encoded if the optional includeChildren is false. The function is in charge of adding the result into the given node and has no return value.
cell | mxCell to be encoded. |
node | Parent XML node to add the encoded cell into. |
includeChildren | Optional boolean indicating if the function should include all descendents. Default is true. |
mxCodec.prototype.isCellCodec = function( codec )
Returns true if the given codec is a cell codec. This uses mxCellCodec.isCellCodec to check if the codec is of the given type.
mxCodec.prototype.decodeCell = function( node, restoreStructures )
Decodes cells that have been encoded using inversion, ie. where the user object is the enclosing node in the XML, and restores the group and graph structure in the cells. Returns a new mxCell instance that represents the given node.
node | XML node that contains the cell data. |
restoreStructures | Optional boolean indicating whether the graph structure should be restored by calling insert and insertEdge on the parent and terminals, respectively. Default is true. |
mxCodec.prototype.setAttribute = function( node, attribute, value )
Sets the attribute on the specified node to value. This is a helper method that makes sure the attribute and value arguments are not null.
node | XML node to set the attribute for. |
attributes | Attributename to be set. |
value | New value of the attribute. |
Constructs an XML encoder/decoder for the specified owner document.
function mxCodec( document )
The owner document of the codec.
mxCodec.prototype.document
Maps from IDs to objects.
mxCodec.prototype.objects
Lookup table for resolving IDs to elements.
mxCodec.prototype.elements
Specifies if default values should be encoded.
mxCodec.prototype.encodeDefaults
Assoiates the given object with the given ID and returns the given object.
mxCodec.prototype.putObject = function( id, obj )
Returns the decoded object for the element with the specified ID in document.
mxCodec.prototype.getObject = function( id )
Hook for subclassers to implement a custom lookup mechanism for cell IDs.
mxCodec.prototype.lookup = function( id )
Returns the element with the given ID from document.
mxCodec.prototype.getElementById = function( id )
Returns the element with the given ID from document.
mxCodec.prototype.updateElements = function()
Adds the given element to elements if it has an ID.
mxCodec.prototype.addElement = function( node )
Returns the ID of the specified object.
mxCodec.prototype.getId = function( obj )
Hook for subclassers to implement a custom method for retrieving IDs from objects.
mxCodec.prototype.reference = function( obj )
Encodes the specified object and returns the resulting XML node.
mxCodec.prototype.encode = function( obj )
Decodes the given XML node.
mxCodec.prototype.decode = function( node, into )
Encoding of cell hierarchies is built-into the core, but is a higher-level function that needs to be explicitely used by the respective object encoders (eg.
mxCodec.prototype.encodeCell = function( cell, node, includeChildren )
Returns true if the given codec is a cell codec.
mxCodec.prototype.isCellCodec = function( codec )
Decodes cells that have been encoded using inversion, ie.
mxCodec.prototype.decodeCell = function( node, restoreStructures )
Inserts the given cell into its parent and terminal cells.
mxCodec.prototype.insertIntoGraph = function( cell )
Sets the attribute on the specified node to value.
mxCodec.prototype.setAttribute = function( node, attribute, value )
Loads the specified URL asynchronously and invokes the given functions depending on the request status.
get: function( url, onload, onerror, binary, timeout, ontimeout, headers )
Parses the specified XML string into a new XML document and returns the new document.
parseXml: function()
Returns the XML content of the specified node.
getXml: function( node, linefeed )
Returns a new, empty XML document.
createXmlDocument: function()
Returns the Id of the cell as a string.
mxCell.prototype.getId = function()
Creates the cell path for the given cell.
create: function( cell )
Returns true since this is a cell codec.
codec.isCellCodec = function()