TreeML简介

原创
2014/03/28 17:46
阅读数 120

前面说到了GraphML,用来存储图。这篇文章介绍TreeML,用来存储树。

不同于GraphML使用XML Schema来表示元数据,TreeML使用DTD(Document Type Definition)来表示元数据。

Tree包括分支(branch)叶子(leaf)。可以推测XML的主要元素就是branch和leaf。我们可能还需要:

1. branch应该是支持嵌套的,因为分支可能还要子分支。

2. branch和leaf需要能够表示相关的数据。

关于第一个,我们可以直接在branch元素里面嵌套branch。

关于第二个,我们可以声明属性。方法如下:

<declarations>
    <attributeDecl name="name" type="String"/>
</declarations>
了解这些规则之后,我们就可以写自己的TreeML了。下面是一个例子:

 <?xml version="1.0" standalone="no"?>
<!DOCTYPE tree [
        <!ELEMENT tree (declarations?, (branch | leaf)* )>
        <!ATTLIST tree
                version CDATA #FIXED "1.0"
                src CDATA #IMPLIED
                desc CDATA #IMPLIED
                date CDATA #IMPLIED
                author CDATA #IMPLIED >
        <!ELEMENT declarations (attributeDecl+) >
        <!ELEMENT attributeDecl EMPTY>
        <!ATTLIST attributeDecl
                name CDATA #REQUIRED
                type (Int|Integer|Long|Float|Real|String|Date|Category) "Int"
                control CDATA #IMPLIED >
        <!ELEMENT branch ( attribute*, (branch | leaf)* ) >
        <!ATTLIST branch
                label CDATA #IMPLIED >
        <!ELEMENT leaf (attribute*) >
        <!ATTLIST leaf
                label CDATA #IMPLIED >
        <!ELEMENT attribute (#PCDATA) >
        <!ATTLIST attribute
                name CDATA #REQUIRED
                value CDATA #REQUIRED >
        ]>
<tree>
    <declarations>
        <attributeDecl name="name" type="String"/>
        <attributeDecl name="number" type="Real"/>
        <attributeDecl name="type" type="String"/>
    </declarations>
    <branch>
        <attribute name="name" value="sample things"/>
        <branch>
            <attribute name="name" value="plants"/>
            <leaf>
                <attribute name="name" value="oak"/>
                <attribute name="number" value="10"/>
                <attribute name="type" value="wild"/>
            </leaf>
            <leaf>
                <attribute name="name" value="afican violet"/>
                <attribute name="number" value="3"/>
                <attribute name="type" value="domestic"/>
            </leaf>
        </branch>
        <branch>
            <attribute name="name" value="animals"/>
            <branch>
                <attribute name="name" value="mammals"/>
                <branch>
                    <attribute name="name" value="felines"/>
                    <leaf>
                        <attribute name="name" value="cat"/>
                        <attribute name="number" value="10"/>
                        <attribute name="type" value="domestic"/>
                    </leaf>
                    <leaf>
                        <attribute name="name" value="lion"/>
                        <attribute name="number" value="3"/>
                        <attribute name="type" value="wild"/>
                    </leaf>
                </branch>
                <branch>
                    <attribute name="name" value="primates"/>
                    <leaf>
                        <attribute name="name" value="human"/>
                        <attribute name="number" value="30"/>
                        <attribute name="type" value="domestic"/>
                    </leaf>
                    <leaf>
                        <attribute name="name" value="gorilla"/>
                        <attribute name="number" value="8"/>
                        <attribute name="type" value="wild"/>
                    </leaf>
                </branch>
                <branch>
                    <attribute name="name" value="primates"/>
                    <leaf>
                        <attribute name="name" value="human"/>
                        <attribute name="number" value="30"/>
                        <attribute name="type" value="domestic"/>
                    </leaf>
                    <leaf>
                        <attribute name="name" value="gorilla"/>
                        <attribute name="number" value="8"/>
                        <attribute name="type" value="wild"/>
                    </leaf>
                </branch>
            </branch>
        </branch>
    </branch>
</tree>

研究TreeML的目的是通过Prefuse来绘制树状图。如果有兴趣可以参考<A title=http://prefuse.org/gallery/treeview/ href="http://prefuse.org/gallery/treeview/">http://prefuse.org/gallery/treeview/。上面的TreeML绘制出来的效果如下:

有的时候,我们的数据可能很大,结果就是TreeML很大。Prefuse提供的public static java.io.InputStream streamFromString(java.lang.String location) 非常不错(位于IOLib)。它会检测输入文件是不是GZip文件,如果是,使用装饰模式,封装成GZipInputStream。这意味着我们可以利用Prefuse来绘制压缩后的TreeML文件。

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部