jGraphX - Import XML and get it's vertices and edges

422 Views Asked by At

I am trying to import an xml file into a graph object and then iterate through its children (vertices && edges). I am trying the following but does not work for me

            String xml = "<root><mxCell> ...... ";
            mxGraph graph = new mxGraph();
            Document document = mxUtils.parseXml(xml);
            mxCodec codec = new mxCodec(document);
            codec.decode(document.getDocumentElement(), graph.getModel());
            Object[] cells = graph.getChildCells(graph.getDefaultParent()); 
            //get all cells 
            for (Object c : cells) {
                mxCell cell = (mxCell) c; 
                if (cell.isVertex()) { 
                   // if is vertex do some things
                }
            }

But the graph.getChildCells(graph.getDefaultParent()) does not return me any cells. Any idea?

=== EDIT ===

In the mxgraph Javascript library I am converting the same xml into a graph object and iterating through it's children (vertices && edges) this way,

let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
let elt = doc.documentElement.firstChild;
while (elt != null)
{ 
    let cell = codec.decode(elt)
    if(cell != undefined){
       if(cell.vertex){
         //is vertex
       }
       else{
         //is edge
       }
       graph.addCell(cell);
    }
    elt = elt.nextSibling;
}

which works perfectly, I am trying to implement the same way in the Java library the following way:

 while (elt != null){
   mxCell mxCell = (mxCell) codec.decode(elt, graph.getModel());
   elt = elt.getNextSibling();           
 }

but casting the Object that codec.decode produces to an mxCell object fails

0

There are 0 best solutions below