deen
deen

Reputation: 2305

mxGraph: Create graph with XML

I am trying to create graph from xml file.

My JavaScript Code is-

function loadXML() {
console.log("Inside loadXML");
var doc = mxUtils.parseXml('<root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="2" value="&lt;img src=&quot;printer.png&quot; onClick=&quot;printer()&quot; style=&quot;width:40px;height:40px;&quot;&gt;&lt;img&gt;" style="shape=image" vertex="1" parent="1"><mxGeometry x="434" y="81" width="40" height="40" as="geometry"/></mxCell></root>');

console.log("XML Parse: " + doc);

var codec = new mxCodec(doc);

console.log("Node Length: " + doc.documentElement.childNodes.length);

var cells = new Array();
for (var i = 0; i < doc.documentElement.childNodes.length; i++) {
    console.log("Node ID: " + i);
    cells[i] = codec.decodeCell(doc.documentElement.childNodes[i]);
}

// import cells into the graph
var delta = mxClipboard.insertCount * mxClipboard.STEPSIZE;
var parent = graph.getDefaultParent();

graph.model.beginUpdate();
console.log("Cells Lenght: " + cells.length);
try
{
  for (var i = 0; i < cells.length; i++)
  {
    cells[i] = graph.importCells([cells[i]], delta, delta, parent)[0];
  }
}
finally
{
  graph.model.endUpdate();
}

// Increments the counter and selects the inserted cells
mxClipboard.insertCount++;
graph.setSelectionCells(cells);

}

In my XML I have configured only one node. But when I am loading my XML into graph, It's taking as two nodes, means for every node it's iterating as two times. Please someone help me what wrong I am doing.

Upvotes: 4

Views: 10594

Answers (4)

Grisha
Grisha

Reputation: 416

I am using a solution inspired by Grapheditor:

const xml = '<mxGraphModel><root>...</root></mxGraphModel>';
const node = mxUtils.parseXml(xml).documentElement;

this.graph.model.beginUpdate();
try {
    this.graph.model.clear();
    dec.decode(node, this.graph.getModel());
} finally {
    this.graph.model.endUpdate();
}

Upvotes: 0

Kishore Vanapalli
Kishore Vanapalli

Reputation: 134

I am using mxGraph in Java for a commandline based integration and this is what i found out

    mxGraph graph = new mxGraph();      
    String strXML = 
    mxUtils.readFile("sample.xml");
    Document xmlGraphDoc = mxXmlUtils.parseXml(strXML);
    mxCodec codec = new mxCodec(xmlGraphDoc);       
    Object o;
    o = codec.decode(xmlGraphDoc.getDocumentElement());
    graph.setModel((mxGraphModel)o);
    mxGraphModel model = (mxGraphModel)graph.getModel();

Below is my Sample XML

<mxGraphModel dx="1422" dy="794" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
  <root>
    <mxCell id="0" />
    <mxCell id="1" parent="0" />
    <mxCell id="7kiFW_6nxtc_rd8VOYzV-1" value="&lt;p style=&quot;margin: 0px ; margin-top: 6px ; text-align: center&quot;&gt;&lt;b&gt;Driver&lt;/b&gt;&lt;/p&gt;&lt;hr&gt;&lt;p style=&quot;margin: 0px ; margin-left: 8px&quot;&gt;&lt;br&gt;&lt;/p&gt;" style="align=left;overflow=fill;html=1;dropTarget=0;" parent="1" vertex="1">
       <mxGeometry x="110" y="90" width="600" height="340" as="geometry" />
    </mxCell>
  </root>
</mxGraphModel>

Upvotes: 0

Ahmed Damasy
Ahmed Damasy

Reputation: 701

if you are using Grapheditor you can use its method (setGraphXml):

let doc = mxUtils.parseXml(xml);

myEditor.editor.setGraphXml(doc.documentElement);

where doc is the parsed XML and myEditor is your editor instance. you can get editor instance with :

const myEditor = new EditorUi(new Editor(urlParams['chrome'] == '1', themes));

Upvotes: 4

Vishal Kamal
Vishal Kamal

Reputation: 1124

try this

 var xml = '<root><mxCell id="2" value="World!Vishal" vertex="1"><mxGeometry x="200" y="150" width="80" height="30" as="geometry"/></mxCell><mxCell id="3" edge="1" source="2"><mxGeometry relative="1" as="geometry"><mxPoint x="440" y="90" as="targetPoint"/></mxGeometry></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.decodeCell(elt));
                        graph.refresh();
                      elt = elt.nextSibling;
                    }

                this.graph.addCells(cells);

Let me know if you have any issue.

Upvotes: 7

Related Questions