Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mxgraph how to export JSON data files

Tags:

mxgraph

I found under the mxgraph GitHub source files exported XML file function is wrong, I would like to have a direct export of JSON data files

like image 664
阿台老师的学生 Avatar asked May 25 '26 01:05

阿台老师的学生


1 Answers

There is a way to get the XML of the graph by downloading xmlToJSON.js from https://github.com/metatribal/xmlToJSON.

Be sure to include this in your HTML file with

<script type="text/javascript" src="xmlToJSON.js"></script>

From there, the following code snippet should convert your XML to JSON

function parseXmlJSON (model)
        {
            var encoder = new mxCodec();
            var node = encoder.encode(graph.getModel());

            var testString = mxUtils.getXml(node);   // fetch xml (string or document/node)
            var result = xmlToJSON.parseString(testString);   // parses to JSON object
            mxUtils.popup(JSON.stringify(result, null, 4), true); // turns into string
        }

Feel free to replace mxUtils.popup with console.log or alert etc.

like image 151
Alexander Hepburn Avatar answered May 27 '26 17:05

Alexander Hepburn