用XML表示树的最佳方式是什么?

时间:2021-04-18 19:37:09

I'm implementing some classes to handle the common data structures (Tree, BinaryTree, Search Binary Tree, BTree, AVL, etc). I'd like a way to store tree information in a data source and parse it to one of those classes. The simplest way is to use XML (portability is very important). The thing is that the XML documents that i make are not easy to read. They're not enough "intuitive". For example, i was doing something like this:

我正在实现一些类来处理公共数据结构(树、二叉树、搜索二叉树、BTree、AVL等)。我希望有一种方法可以将树信息存储在数据源中,并将其解析为其中的一个类。最简单的方法是使用XML(可移植性非常重要)。我做的XML文档并不容易阅读。他们没有足够的“直觉”。例如,我做了这样的事情:

<?xml version="1.0" encoding="UTF-8"?>
<tree>
    <root>100</root>
    <node>
        <tree>
            <root>50</root>     
        </tree>
    </node>
    <node>
        <tree>
            <root>200</root>        
        </tree>
    </node>
</tree>

There, the tree would be something like this:

树是这样的:

                    100
              50                200

What do you say? Do you have any other recommendation?

你说什么?你还有什么别的建议吗?

4 个解决方案

#1


5  

I don't see in your description any important structural difference between the root, leaves of the tree, and other nodes. So they could all have the same content model, as in this example:

在你的描述中,我看不出树的根、叶和其他节点之间有什么重要的结构差异。所以他们都可以有相同的内容模型,就像这个例子一样:

<tree>
    <value>100</value>
    <tree>
        <value>50</value>
    </tree>
    <tree>
        <value>200</value>
    </tree>
</tree>

This structure is a bit less verbose, and the XML document tree is matching the tree you are trying to represent. If your values can be expressed as strings, you could even write it like that:

这个结构稍微不那么冗长,XML文档树与要表示的树匹配。如果你的值可以用字符串表示,你甚至可以这样写:

<tree>
    100
    <tree>50</tree>
    <tree>200</tree>
</tree>

#2


2  

There is no 'best' way to represent a tree in XML. It all depends what you want to optimize your storage for - do you want to be able to track down a specific node quickly? Then a flat(er) representation might be better. If you want to keep the description non-verbose, then the method you outlined will be pretty good (though I would probably turn the 'root' child into an attribute (root/value), that way every node in the xml is actually a tree node).

用XML表示树没有最好的方法。这完全取决于您想要优化存储的目的——您希望能够快速地跟踪特定的节点吗?那么扁平化(er)表示可能更好。如果您想保持描述不冗长,那么您所描述的方法将非常好(尽管我可能会将“root”子节点转换为属性(root/value),这样xml中的每个节点实际上都是树节点)。

#3


1  

<node value="100">
    <node value="50" />
    <node value="200" />
</node>

Elements in the tree are nodes, not trees. The whole node assembly constitues a tree.

树中的元素是节点,而不是树。整个节点组装构成了一个树。

Also, XML is a tree itself, so you don't have to explicitly model it.

而且,XML本身就是树,所以您不必显式地建模它。

#4


0  

In my opinion, data structures are meant to be easy on the computer, not a human. If you want a human readable version of the data, just create a pretty printing visitor.

在我看来,数据结构应该在计算机上很容易,而不是人。如果您想要一个人类可读的数据版本,只需创建一个漂亮的打印访问者。

#1


5  

I don't see in your description any important structural difference between the root, leaves of the tree, and other nodes. So they could all have the same content model, as in this example:

在你的描述中,我看不出树的根、叶和其他节点之间有什么重要的结构差异。所以他们都可以有相同的内容模型,就像这个例子一样:

<tree>
    <value>100</value>
    <tree>
        <value>50</value>
    </tree>
    <tree>
        <value>200</value>
    </tree>
</tree>

This structure is a bit less verbose, and the XML document tree is matching the tree you are trying to represent. If your values can be expressed as strings, you could even write it like that:

这个结构稍微不那么冗长,XML文档树与要表示的树匹配。如果你的值可以用字符串表示,你甚至可以这样写:

<tree>
    100
    <tree>50</tree>
    <tree>200</tree>
</tree>

#2


2  

There is no 'best' way to represent a tree in XML. It all depends what you want to optimize your storage for - do you want to be able to track down a specific node quickly? Then a flat(er) representation might be better. If you want to keep the description non-verbose, then the method you outlined will be pretty good (though I would probably turn the 'root' child into an attribute (root/value), that way every node in the xml is actually a tree node).

用XML表示树没有最好的方法。这完全取决于您想要优化存储的目的——您希望能够快速地跟踪特定的节点吗?那么扁平化(er)表示可能更好。如果您想保持描述不冗长,那么您所描述的方法将非常好(尽管我可能会将“root”子节点转换为属性(root/value),这样xml中的每个节点实际上都是树节点)。

#3


1  

<node value="100">
    <node value="50" />
    <node value="200" />
</node>

Elements in the tree are nodes, not trees. The whole node assembly constitues a tree.

树中的元素是节点,而不是树。整个节点组装构成了一个树。

Also, XML is a tree itself, so you don't have to explicitly model it.

而且,XML本身就是树,所以您不必显式地建模它。

#4


0  

In my opinion, data structures are meant to be easy on the computer, not a human. If you want a human readable version of the data, just create a pretty printing visitor.

在我看来,数据结构应该在计算机上很容易,而不是人。如果您想要一个人类可读的数据版本,只需创建一个漂亮的打印访问者。

相关文章