2
我正在嘗試從xml文件創建圖形。mxGraph:用XML創建圖形
我的JavaScript代碼是 -
function loadXML() {
console.log("Inside loadXML");
var doc = mxUtils.parseXml('<root><mxCell id="0"/><mxCell id="1" parent="0"/><mxCell id="2" value="<img src="printer.png" onClick="printer()" style="width:40px;height:40px;"><img>" 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);
}
在我的XML我也只配置了一個節點。但是,當我將XML加載到圖中時,它將以兩個節點的形式表示每個節點的迭代次數。請有人幫我解決我在做什麼錯。
謝謝,工作像魅力 – deen