Pablo
Pablo

Reputation: 10660

Export mxGraph to SVG (Or any type of image)

I have implemented a diagram editor with mxGraph with javascript, (Same as the one in the example provided by them), I can get an XML, here I let you an example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mxGraphModel connect="1" fold="1" grid="1" gridSize="10" guides="1" page="0"    pageHeight="1169" pageScale="1" pageWidth="826" tooltips="1">
    <root>
        <mxCell id="0"/>
        <mxCell id="1" parent="0"/>
        <mxCell id="2" parent="1" style="whiteSpace=wrap" value="" vertex="1">
            <mxGeometry as="geometry" height="60" width="120" x="80" y="70"/>
        </mxCell>
        <mxCell id="3" parent="1" style="whiteSpace=wrap" value="" vertex="1">
            <mxGeometry as="geometry" height="60" width="120" x="280" y="70"/>
        </mxCell>
        <mxCell edge="1" id="4" parent="1" source="2" style="edgeStyle=orthogonalEdgeStyle;rounded=0;" target="3">
            <mxGeometry as="geometry" relative="1"/>
        </mxCell>
    </root>
</mxGraphModel>

I generate this xml from the javascript editor and sending it to a java class with ajax.

I can't find a way to save an image svg (or any other kind of image), based on the xml I get on the java class.

All the examples I find on internet shows how to export an image based on the mxgraph created directly on java, but not how to get it from the xml

Upvotes: 2

Views: 5983

Answers (1)

Bala
Bala

Reputation: 1365

You can use mxXmlUtils.parseXml api. Here is the sample code snippet

Document doc = mxXmlUtils.parseXml(erXmlString);
    mxGraph graph = new mxGraph();
    mxCodec codec = new mxCodec(doc);
    codec.decode(doc.getDocumentElement(), graph.getModel());

    mxGraphComponent graphComponent = new mxGraphComponent(graph);
    BufferedImage image = mxCellRenderer.createBufferedImage(graphComponent.getGraph(), null, 1, Color.WHITE, graphComponent.isAntiAlias(), null, graphComponent.getCanvas());
    mxPngEncodeParam param = mxPngEncodeParam.getDefaultEncodeParam(image);
    param.setCompressedText(new String[] { "mxGraphModel", erXmlString });

    FileOutputStream outputStream = new FileOutputStream(new File(filename));
    mxPngImageEncoder encoder = new mxPngImageEncoder(outputStream, param);

    if (image != null) {
        encoder.encode(image);
        return image;
    }
    outputStream.close();
    return null;
}

Upvotes: 3

Related Questions