tags:

views:

86

answers:

3

In the world of XML, is it better to leave blank elements in the file:

<widgets>
    <widget>
        <id>5</id>
        <name>Bob</name>
        <price>5.33</price>
        <otherInfo>Bob is a ball.</otherInfo>
        <dateAdded>9-5-2010</dateAdded>
    </widget>
    <widget>
        <id>3</id>
        <name>Mary</name>
        <price>4.67</price>
        <otherInfo></otherInfo>
        <dateAdded>10-1-2010</dateAdded>
    </widget>
</widgets>

Or remove them:

<widgets>
    <widget>
        <id>5</id>
        <name>Bob</name>
        <price>5.33</price>
        <otherInfo>Bob is a ball.</otherInfo>
        <dateAdded>9-5-2010</dateAdded>
    </widget>
    <widget>
        <id>3</id>
        <name>Mary</name>
        <price>4.67</price>
        <dateAdded>10-1-2010</dateAdded>
    </widget>
</widgets>

For parsing, it'd be easier if they were there, since there wouldn't be a need to check if the element existed before trying to fetch it. On the other hand, the XML file would not be littered with blank elements.

Does best practices dictate one form of storage vs. the other, or does it depend on what data is being stored?

A: 

I don't think there is a standard on this.

If you are doing the parsing as well as creating the XML file, I would go with whatever is easier for you. Changing the code or maintaining a more or less easy to understand file.

If other people will be creating the XML file then I would say for a list few fields require blanks and for many fields allow some to be removed.

But remember, if the field is not there, nobody will no it exists unless they read documentatioon

cjavapro
just not true. blank and self closing tags do have a meaning that depending on the applicatin design needs to be preservedo or not
Joe Hopfgartner
+2  A: 

It depends on what data is being stored.

For some elements, just the presence of the element makes a huge difference - a simple example is <hr /> or <br /> in HTML. For other data formats, it makes no difference.

You should usually know what kind of data you're dealing with - and part of understanding the data format is understanding whether or not empty elements are important.

Jon Skeet
In this case, the element being empty / not being there would both indicate that the information for that element for that entry does not exist. I'm writing both the format of the document and the parser myself, so I'm in complete control of how to deal with specific elements being/not being there.
Secret Agent Man
+1  A: 

It depends on the application. A blank tag DOES have a meaning and its NOT the same if it ISNT THERE. Depending on the application design it needs to be preserved or not.

I have seen a lot of application where blank and/or self closing tags (<otherInfo/>) have had a meaning in an application and if they wouldn't have been there the application would have stopped working.

Just to give you an example: In magento some cache backends require a cache prefix. If its blank its a blank prefix, but if it isn't there it will just not work. That would be one example of what you cann "blank" tags.

Another application i have worked with was a catalog API for a set-top-box video on demand provider.

there was a node just <itempurchased/> to indicate that the item has been bought and could be served. i personally don't like this design because its ambigous. I would design it like <itempurchased>true</itempurchased> or <itempurchased bought="true/> or something but you thats the world of working together :).

So they DO contain a value. It is a string of length zero.

If you don't want to change the data, you have to preserve empty and self closing tags.

Joe Hopfgartner
You're making some assumptions here, depending on what you're doing, say serializing and object to XML and back (again, depending on platform) it may *not* matter if it's there or not. It really depends on what you're doing with the XML here.
Nick Craver
no, YOU are making assumption. In the "world of xml" it matters. if it makes no difference to your application, well thats your thing. i just gave a specific example where it DOES matter. therefore impliing that it MATTERS in general.
Joe Hopfgartner
What *shouldn't* matter is whether you use `<foo/>` or `<foo></foo>`; in XML, they're officially identical. Alas, some people insist on using regular expressions to parse XML and that's one of the things they choke on…
Donal Fellows
thats true and thats why i always mention both versions "blank and/or self closing tags" ... " they DO contain a value" but thanks for pointing it out once mor +1
Joe Hopfgartner