Rendering XML from draw.io as an image using mxCellRenderer

I am trying to programmatically read in an XML file created by draw.io, an online charting / charting service. Draw.io is built using mxGraph in its core, which was recently named externally by jgraphx (thus, the tag on this post), although the class names remain the same.

https://stackoverflow.com/questions/1242554/ ... shows how to read raw XML data from a file and convert it to an mxGraph object, and this mxGraph Javadocs page describes how to convert from an mxGraph object to a rendered image.

Unfortunately for me, despite the fact that, following both guidelines, the image that is "rendered" is always null, and an IllegalArgumentException (because the image is null). My code is as follows:

 String xmlFile = "work/test.xml"; String imageFile = "work/test.png"; mxGraph graph = new mxGraph(); try { Document doc = mxXmlUtils.parseXml(mxUtils.readFile(xmlFile)); mxCodec codec = new mxCodec(doc); codec.decode(doc.getDocumentElement(), graph.getModel()); } catch (IOException e) { e.printStackTrace(); } RenderedImage image = mxCellRenderer.createBufferedImage(graph, null, 1, \\ Color.WHITE, false, null); try { ImageIO.write(image, "png", new File(imageFile)); } catch (IOException e) { e.printStackTrace(); } 

As you can see, this code should read the XML data, create an mxGraph object from this data, and then render the mxGraph object as an image in the current working directory. Instead, however, nothing happens and no image is created.

Has anyone ever experienced this? Am I missing something? Is there a better way to do what I'm trying to do? Any help would be appreciated.

FYI, here is Pastebin with a sample XML file if you want to try it for yourself.

+9
source share
1 answer

With some help from the guys at draw.io support, I found the answer: XML is confused, yes, but not irrevocably. It is simply compressed and needs to be unpacked. For this:

  1. Base64 Decoding
  2. inflate
  3. URL decoding

I found this link that does all 3 of the above steps in one fell swoop: https://jgraph.imtqy.com/drawio-tools/tools/convert.html .

When I had the unpacked XML, my code worked fine and generated the expected result.

An example implementation is here: https://github.com/laingsimon/render-diagram/blob/master/drawio-renderer/src/main/java/com/simonlaing/drawiorenderer/models/DiagramDecoder.java

+10
source

Source: https://habr.com/ru/post/1242551/


All Articles