简体   繁体   中英

Read/Parse Custom Xml in the mxgraph

I have the custom xml created from the third party application which is data(diagram) about the electronic data.Now i want to rewrite the same data in mxgraph.
Please find the sample xml of thirdy party application

    <component type="Rerror" mirrored="0" typeid="57" nodematch="0" macrotype="2" position="280,260" orientation="0">
  <parameter idx="0" encrypted="0">
data unrelated to drawing here
  </parameter>
  <image>
    <hotzone points="60,-10,-60,10"/>
    <shape type="line" width="1" color="#0000ff" points="60,0,40,0"/>
    <shape type="line" width="1" color="#0000ff" points="40,0,30,-10"/>
    <shape type="line" width="1" color="#0000ff" points="30,-10,10,10"/>
    <shape type="line" width="1" color="#0000ff" points="10,10,-10,-10"/>
    <shape type="line" width="1" color="#0000ff" points="-10,-10,-30,10"/>
    <shape type="line" width="1" color="#0000ff" points="-30,10,-40,0"/>
    <shape type="line" width="1" color="#0000ff" points="-40,0,-60,0"/>
    <legend text="0.332 MOhm" position="-36,20" orientation="0" fontname="Arial" fontsize="12" bold="0" italic="0" color="#229922"/>
    <pin position="-60,0" dir="0" length="0" text="6"/>
    <pin position="60,0" dir="0" length="0" text="3"/>
    <label text="Rerror" position="250,236" textroot=" " orientation="0" color="#228822"/>
  </image>
</component>

The above xml has to parsed and redrawn using the mxgraph. Where

  • component : is the whole image of particalar unit (there can be many components

  • parameter : this section should be igonred

  • image: image part contians the data about of the x,y cordinates which has to drawn in the graph


I was going through the mxgraph user guide & i am new to mxgraph.I was able to see normal prase of the xml using Please help me with how to parse custom file看到 xml 的正常请帮助我如何解析自定义文件

mxGraph doesn't support custom xml support out of the box. In fact there is no such tool to convert custom into mxGraph readable xml. Recently we have tried converting our custom xmls into graphs using xsds. Using xsd approach is the better way to deal with xmls. Try following steps

  • If you have xsd for your custom xml, then create java classes out of it.(Your xml looks simpler, if you don't have an xsd, you can create it).
  • Get mxGraph xsd from internet.(Though they officially didn't publish any xsd, there are versions available on internet).
  • Read your data from custom xml java classes and fill it to mxGraph-xml-java classes.

As simple as that.

parseXML() will only make it easier to use in the code (creates a DOM document if you want) but you'll have to write your "converter" manually.

for each <shape> of your file, create a mxCell. Create different styles for your various cells and you're done.

If all your shapes are rectangle it should be fairly straightforward!

  1. Convert your xml to java objects. This is unrelated to JGraph. If the .xsd is available, you can use JAXB for example.

  2. Once you have the java objects from your xml, you can use the information contianed in them to create edges and vertices. In your JGraph based application, the mxGraph class has methods insertVertex and insertEdge , which will take your positions and labels as parameters. You can for example loop throught the java objects parsed from the XML and call graph.insertVertex() for each.

  3. If the mxGraph object is not directly available, you can use mxGraphComponent.getGraph() to reach it. mxGraphComponent should be obtainable from the editor of your choice, ie. editor.getGraphComponent() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM