简体   繁体   English

在 mxgraph 中读取/解析自定义 Xml

[英]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.我有从第三方应用程序创建的自定义 xml,它是关于电子数据的数据(图表)。现在我想在 mxgraph 中重写相同的数据。
Please find the sample xml of thirdy party application请找到第三方应用的示例xml

    <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.上面的 xml 必须使用 mxgraph 解析和重绘。 Where在哪里

  • component : is the whole image of particalar unit (there can be many components component : 是特定单元的整体图像(可以有很多组件

  • 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图像:图像部分包含必须在图形中绘制的 x,y 坐标的数据


I was going through the mxgraph user guide & i am new to mxgraph.I was able to see normal prase of the xml using "mxUtils.parseXml('location')" Please help me with how to parse custom file我正在阅读 mxgraph 用户指南 & 我是 mxgraph 的新手。我能够使用“mxUtils.parseXml('location')”看到 xml 的正常prase请帮助我如何解析自定义文件

mxGraph doesn't support custom xml support out of the box. mxGraph 不支持开箱即用的自定义 xml 支持。 In fact there is no such tool to convert custom into mxGraph readable xml.实际上没有这样的工具可以将自定义转换为 mxGraph 可读的 xml。 Recently we have tried converting our custom xmls into graphs using xsds.最近我们尝试使用 xsds 将我们的自定义 xml 转换为图形。 Using xsd approach is the better way to deal with xmls.使用 xsd 方法是处理 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).如果您有自定义 xml 的 xsd,然后从中创建 java 类。(您的 xml 看起来更简单,如果您没有 xsd,则可以创建它)。
  • Get mxGraph xsd from internet.(Though they officially didn't publish any xsd, there are versions available on internet).从 Internet 获取 mxGraph xsd。(虽然他们官方没有发布任何 xsd,但 Internet 上有可用的版本)。
  • Read your data from custom xml java classes and fill it to mxGraph-xml-java classes.从自定义 xml java 类中读取数据并将其填充到 mxGraph-xml-java 类中。

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. parseXML()只会使它更容易在代码中使用(如果需要,可以创建一个 DOM 文档),但您必须手动编写“转换器”。

for each <shape> of your file, create a mxCell.对于文件的每个<shape> ,创建一个 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.将您的 xml 转换为 java 对象。 This is unrelated to JGraph.这与 JGraph 无关。 If the .xsd is available, you can use JAXB for example.如果 .xsd 可用,您可以使用 JAXB 例如。

  2. Once you have the java objects from your xml, you can use the information contianed in them to create edges and vertices.从 xml 中获得 java 对象后,就可以使用其中包含的信息来创建边和顶点。 In your JGraph based application, the mxGraph class has methods insertVertex and insertEdge , which will take your positions and labels as parameters.在基于 JGraph 的应用程序中, mxGraph类具有方法insertVertexinsertEdge ,它们将您的位置和标签作为参数。 You can for example loop throught the java objects parsed from the XML and call graph.insertVertex() for each.例如,您可以遍历从 XML 解析的 java 对象,并为每个对象调用graph.insertVertex()

  3. If the mxGraph object is not directly available, you can use mxGraphComponent.getGraph() to reach it.如果mxGraph对象不是直接可用的,您可以使用mxGraphComponent.getGraph()来访问它。 mxGraphComponent should be obtainable from the editor of your choice, ie. mxGraphComponent应该可以从您选择的编辑器中获取,即。 editor.getGraphComponent() . editor.getGraphComponent()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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