tags:

views:

66

answers:

2

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:

One section in the xml file is:

<ObjectType>

which can be Blur, Sharpen, Smooth, etc.

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>

or

<a>Type=something</a>
+5  A: 

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.

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>

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.

Welbog
+1: Beat me to it.
Ben S
Thanks what's xsd and dtd? So #2 is only good for boolean stuff? #1 is the most recommended way then, right? How would you store the Blur filter itself, as in Filter=Blur? or a nested field?
Joan Venge
@Joan Venge: XSD and DTD are standard methods of validating XML. I'll add links to them from the question. I would say that the third method (the middle ground) is the more generic method. All of the methods can work for non-boolean values. For example, you could have `<GaussianRadius>5</GaussianRadius>` which is an integer. I don't know what you mean about storing the blur filter itself. What part of `<BlurType>...</BlurType>` is insufficient?
Welbog
Thanks Welbog. I meant storing the whole blur, as in you stored only the BlurType. Where would BlurType sit inside the Blur, or you mean BlurType==Blur?
Joan Venge
I don't really know what you mean. I'm extrapolating from the example that you gave, in which the element was named `BlurType`. If the object you want to create is called `Blur`, then the element should be called `Blur`. If it's called `Blur`, why were you using the element `BlurType` in your example?
Welbog
Or would you make it like <Filter Type="Blur"> (blur type here) </Filter?
Joan Venge
I just wanted to give an example to sub options for Filters, in that case it was Blur.
Joan Venge
So blur has blur options, sharpen has no options, etc.
Joan Venge
You need to explain yourself better. Based on my understanding of what you want to accomplish, I've given you everything you need.
Welbog
+1  A: 

Either

<ObjectType name="Blur" type="Gaussian" />

or

<ObjectType>
    <Name>Blur</Name>
    <Type>Gaussian</Type>
</ObjectType>

would be my first 2 choices.

David