如何设计在 xml 中存储复杂的对象设置 [英] How to design storing complex object settings in an xml
问题描述
我正在尝试将每个对象状态的自定义设置存储在对象本身上,但我不知道如何以面向对象的方式执行此操作.所以:
I am trying to store custom settings for each objects state on the object itself, but I don't know how I can do this in an object-oriented way. So:
xml 文件中的一个部分是:
One section in the xml file is:
<ObjectType>
可以是Blur, Sharpen, Smooth,
等
但是如果 Blur
的类型是 Blur
,那么它有额外的属性要存储,比如:
But say that Blur
has additional properties to be stored only if the type is Blur
, like:
<BlurType>Gaussian, Smart, etc</BlurType>
此外,我不确定是否应该将这些存储为:
Also I am not sure if I should store these as:
<a>something</a>
或
<a>Type=something</a>
推荐答案
永远不要在 XML 中存储分隔数据.
也就是说,永远不要有包含逗号分隔值列表、等号分隔值或类似内容的元素.XML 为您提供了创建列表的机制,如果您打算使用它,则不应重新发明它.
Never store delimited data in XML.
That is to say, never have an element that contains a list of comma-separated values, or equals-sign-separated values, or anything like that. XML provides for you the mechanism to create lists and you should not reinvent that if you're planning on using it.
XML 方式"像您所说的那样存储列表是这样的:
The "XML way" to store a list like you're talking about is like this:
<BlurType>
<option>Gaussian</option>
<option>Smart</option>
...
</BlurType>
或者,如果您有一组可以打开或关闭的特定值,
Or, if you have a set of specific values that you can either turn on or off,
<BlurType>
<Gaussian>true</Gaussian>
<Smart>true</Smart>
...
</BlurType>
第二种方法可以比第一种方法更严格地针对 XSD 进行验证.第一个更灵活.您不想在 XML 中的任何地方存储诸如 type=something
之类的东西.两者之间的中间立场是这样的:
The second method can be validated against an XSD more strictly than the first. The first is more flexible. You don't want to be storing things like type=something
anywhere in XML. A middle ground between the two would work like this:
<BlurType>
<option name="Gaussian">true</option>
<option name="Smart">true</option>
...
</BlurType>
This offers flexibility and at the same time you can validate the values of the name
attribute and option
elements against an XSD or DTD.
这篇关于如何设计在 xml 中存储复杂的对象设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!