首页 > 解决方案 > 用 XML 描述图

问题描述

我有一个如下图所示的图表。 图模式

如何在 XML 文件中描述此图?我考虑某种图形元素的上一个和下一个元素的行列列表。最简单的方法是什么?

标签: xmlgraph

解决方案


有无数种方法可以做到这一点。例如,如下所示:

<graph>
  <nodes>
    <node type="box" id="n1" name="1"/>
    <node type="box" id="n2" name="2"/>
    <node type="box" id="n3" name="3"/>
    <node type="box" id="n4" name="4"/>
    <node type="box" id="n5" name="5"/>
    <node type="box" id="n6" name="6"/>
    <node type="box" id="n7" name="7"/>
    <node type="box" id="n8" name="8"/>
    <node type="box" id="n9" name="9"/>
    <node type="box" id="n10" name="10"/>
    <node type="box" id="n11" name="11"/>

    <node type="null" id="n12"/>
    <node type="null" id="n13"/>

    <node type="junction" id="n14"/>
    <node type="junction" id="n15"/>
    <node type="junction" id="n16"/>
    <node type="junction" id="n17"/>
    <node type="junction" id="n18"/>
    <node type="junction" id="n19"/>
  </nodes>
  <edges>
    <edge from="n12" to="n1"/>
    <edge from="n1" to="n14"/>
    <edge from="n14" to="n2"/>
    <edge from="n14" to="n3"/>
    <edge from="n2" to="n15"/>
    <edge from="n3" to="n15"/>
    <edge from="n15" to="n16"/>
    <edge from="n16" to="n4"/>
    <edge from="n16" to="n7"/>
    <edge from="n16" to="n8"/>
    <edge from="n4" to="n17"/>
    <edge from="n17" to="n5"/>
    <edge from="n17" to="n6"/>
    <edge from="n5" to="n18"/>
    <edge from="n6" to="n18"/>
    <edge from="n18" to="n19"/>
    <edge from="n7" to="n19"/>
    <edge from="n8" to="n9"/>
    <edge from="n9" to="n19"/>
    <edge from="n19" to="n10"/>
    <edge from="n10" to="n11"/>
    <edge from="n11" to="n13"/>
  </edges>
</graph>

但是,它仍然没有捕获节点的位置和边缘的形状。

通过以下生成文件的xsh脚本进行验证:

echo 'graph { rankdir=LR;' ;
open 1.xml ;
my $node_tally = count(/graph/nodes/node) ;

for my $node in /graph/nodes/node {
    if ($node/@type = 'box') {
        echo :s $node/@id '[label="' $node/@name '"]' ;
    } else {
        echo :s $node/@id
            '[style=invis,label="",fixedsize=true,height=0,width=0]' ;
    }
}

for my $edge in /graph/edges/edge {
    my $from = /graph/nodes/node[@id=$edge/@from] ;
    my $to   = /graph/nodes/node[@id=$edge/@to] ;
    echo :s '"' $from/@id '"' ' -- ' '"' $to/@id '"' ;
}

echo '}' ;

点文件生成以下图片:

生成的图


推荐阅读