Should empty XML elements be deleted?

In the XML world, it's best to leave blank elements in a 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 delete 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> 

It would be easier for parsing if they were there, since there would be no need to check if this element existed before trying to extract it. On the other hand, the XML file will not be cluttered with empty elements.

Does best practice use one kind of storage or another, or depends on what data is stored?

+4
source share
3 answers

It depends on what data is stored.

For some elements, just having an element matters a lot - a simple example: <hr /> or <br /> in HTML. For other data formats, this does not matter.

Usually you need to know what data you are dealing with, and part of understanding the data format is whether empty elements are important.

+3
source

It depends on the application. An empty tag matters, but it is NOT the same if it DOES NOT HAVE THERE. Depending on the design of the application, you need to save it or not.

I saw many applications where empty and / or closing tags ( <otherInfo/> ) made sense in the application, and if they were not there, the application would stop working.

Just to give an example: In magento, some cache servers require a cache prefix. If its empty is an empty prefix, but if it is not there, it just won't work. This will be one example of how you can use empty tags.

Another application I worked with was the directory API for the on-demand video provider.

there was node only <itempurchased/> to indicate that the item was purchased and could be served. I personally do not like this design, because it is unique. I would design it as <itempurchased>true</itempurchased> or <itempurchased bought="true/> or something else, but you are a world of collaboration :).

Therefore, they DO contain a value. This is a string with zero length .

If you do not want to change the data, you need to keep empty and closing tags.

+2
source

I do not think that there is a standard on this.

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

If other people will create an XML file, then I would say that several fields require spaces, and many fields can be deleted.

But remember that if there is no field, no one will, unless they read the documentation

-2
source

All Articles