Options
All
  • Public
  • Public/Protected
  • All
Menu

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.

References

In order to resolve references, especially forward references, the mxCodec constructor must be given the document that contains the referenced elements.

Examples

The following code is used to encode a graph model.

example
var encoder = new mxCodec();
var result = encoder.encode(graph.getModel());
var xml = mxUtils.getXml(result);

Example

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.

example
var doc = mxUtils.parseXml(xmlString);
var codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());

Example

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.

example
var 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>';
var doc = mxUtils.parseXml(xml);
var codec = new mxCodec(doc);
var elt = doc.documentElement.firstChild;
var cells = [];

while (elt != null)
{
  cells.push(codec.decode(elt));
  elt = elt.nextSibling;
}

graph.addCells(cells);

Example

Using the following code, the selection cells of a graph are encoded and the output is displayed in a dialog box.

example
var enc = new mxCodec();
var cells = graph.getSelectionCells();
mxUtils.alert(mxUtils.getPrettyXml(enc.encode(cells)));

Newlines in the XML can be converted to
, in which case a '
' argument must be passed to mxUtils.getXml as the second argument.

Debugging

For debugging I/O you can use the following code to get the sequence of encoded objects:

example
var oldEncode = encode;
encode(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:

example
var MyObj(name)
{
  // ...
};

Hierarchy

  • mxCodec

Index

Constructors

constructor

  • new mxCodec(document?: XMLDocument): mxCodec

Properties

document

document: XMLDocument

The owner document of the codec.

elements

elements: any[]

Lookup table for resolving IDs to elements.

encodeDefaults

encodeDefaults: boolean

Specifies if default values should be encoded. Default is false.

objects

objects: string[]

Maps from IDs to objects.

Methods

addElement

  • addElement(node: Node): void

decode

  • decode(node: Node, into?: any): any
  • 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.

    Parameters

    • node: Node

      XML node to be decoded.

    • Optional into: any

      Optional object to be decodec into.

    Returns any

decodeCell

  • decodeCell(node: Node, restoreStructures?: boolean): mxCell
  • 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.

    Parameters

    • node: Node

      XML node that contains the cell data.

    • Optional restoreStructures: boolean

      Optional boolean indicating whether the graph structure should be restored by calling insert and insertEdge on the parent and terminals, respectively. Default is true.

    Returns mxCell

encode

  • encode(obj: any): XMLDocument
  • Encodes the specified object and returns the resulting XML node.

    Parameters

    • obj: any

      Object to be encoded.

    Returns XMLDocument

encodeCell

  • encodeCell(cell: mxCell, node: Node, includeChildren?: boolean): void
  • 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. {@link mxModelCodec}, {@link mxChildChangeCodec} and {@link 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.

    Parameters

    • cell: mxCell

      mxCell to be encoded.

    • node: Node

      Parent XML node to add the encoded cell into.

    • Optional includeChildren: boolean

      Optional boolean indicating if the function should include all descendents. Default is true.

    Returns void

getElementById

  • getElementById(id: string): Element
  • Returns the element with the given ID from document.

    Parameters

    • id: string

      String that contains the ID.

    Returns Element

getId

  • getId(obj: any): string
  • 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.

    Parameters

    • obj: any

      Object to return the ID for.

    Returns string

getObject

  • getObject(id: string): any
  • Returns the decoded object for the element with the specified ID in document. If the object is not known then lookup is used to find an object. If no object is found, then the element with the respective ID from the document is parsed using decode.

    Parameters

    • id: string

    Returns any

insertIntoGraph

  • insertIntoGraph(cell: mxCell): void
  • Inserts the given cell into its parent and terminal cells.

    Parameters

    Returns void

isCellCodec

  • isCellCodec(codec: mxCodec): boolean
  • Returns true if the given codec is a cell codec. This uses {@link mxCellCodec.isCellCodec} to check if the codec is of the given type.

    Parameters

    Returns boolean

lookup

  • lookup(id: string): any
  • Hook for subclassers to implement a custom lookup mechanism for cell IDs. This implementation always returns null.

    Example:

    var codec = new mxCodec();
    codec.lookup(id)
    {
      return model.getCell(id);
    };
    

    Parameters

    • id: string

      ID of the object to be returned.

    Returns any

putObject

  • putObject(id: string, obj: any): any
  • Assoiates the given object with the given ID and returns the given object.

    Parameters

    • id: string

      ID for the object to be associated with.

    • obj: any

      Object to be associated with the ID.

    Returns any

reference

  • reference(obj: any): any
  • Hook for subclassers to implement a custom method for retrieving IDs from objects. This implementation always returns null.

    Example:

    var codec = new mxCodec();
    codec.reference(obj)
    {
      return obj.getCustomId();
    };
    

    Parameters

    • obj: any

      Object whose ID should be returned.

    Returns any

setAttribute

  • setAttribute(node: Node, attribute: string, value: any): void
  • 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.

    Parameters

    • node: Node

      XML node to set the attribute for.

    • attribute: string
    • value: any

      New value of the attribute.

    Returns void

updateElements

  • updateElements(): void

Generated using TypeDoc