簡體   English   中英

在 mxgraph 中讀取/解析自定義 Xml

[英]Read/Parse Custom Xml in the mxgraph

我有從第三方應用程序創建的自定義 xml,它是關於電子數據的數據(圖表)。現在我想在 mxgraph 中重寫相同的數據。
請找到第三方應用的示例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>

上面的 xml 必須使用 mxgraph 解析和重繪。 在哪里

  • component : 是特定單元的整體圖像(可以有很多組件

  • 參數:這部分應該被忽略

  • 圖像:圖像部分包含必須在圖形中繪制的 x,y 坐標的數據


我正在閱讀 mxgraph 用戶指南 & 我是 mxgraph 的新手。我能夠使用“mxUtils.parseXml('location')”看到 xml 的正常prase請幫助我如何解析自定義文件

mxGraph 不支持開箱即用的自定義 xml 支持。 實際上沒有這樣的工具可以將自定義轉換為 mxGraph 可讀的 xml。 最近我們嘗試使用 xsds 將我們的自定義 xml 轉換為圖形。 使用 xsd 方法是處理 xmls 的更好方法。 嘗試以下步驟

  • 如果您有自定義 xml 的 xsd,然后從中創建 java 類。(您的 xml 看起來更簡單,如果您沒有 xsd,則可以創建它)。
  • 從 Internet 獲取 mxGraph xsd。(雖然他們官方沒有發布任何 xsd,但 Internet 上有可用的版本)。
  • 從自定義 xml java 類中讀取數據並將其填充到 mxGraph-xml-java 類中。

就如此容易。

parseXML()只會使它更容易在代碼中使用(如果需要,可以創建一個 DOM 文檔),但您必須手動編寫“轉換器”。

對於文件的每個<shape> ,創建一個 mxCell。 為您的各種單元格創建不同的樣式,您就完成了。

如果您的所有形狀都是矩形,那應該相當簡單!

  1. 將您的 xml 轉換為 java 對象。 這與 JGraph 無關。 如果 .xsd 可用,您可以使用 JAXB 例如。

  2. 從 xml 中獲得 java 對象后,就可以使用其中包含的信息來創建邊和頂點。 在基於 JGraph 的應用程序中, mxGraph類具有方法insertVertexinsertEdge ,它們將您的位置和標簽作為參數。 例如,您可以遍歷從 XML 解析的 java 對象,並為每個對象調用graph.insertVertex()

  3. 如果mxGraph對象不是直接可用的,您可以使用mxGraphComponent.getGraph()來訪問它。 mxGraphComponent應該可以從您選擇的編輯器中獲取,即。 editor.getGraphComponent()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM