I haven't worked with XML files all that much. But I'm writing an app now that saves its data in XML format.
So far, I've come up with the following basic structure.
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category id="cf6cb6bc-8142-4366-9b43-0ae6fce90df2">
<subcategories>
<subcategory id="02b95e55-a4f7-4979-b0aa-a97f2c3327b0">
<articles>
<article ... />
<article ... />
</articles>
</subcategory>
<subcategory id="5e9f9ef7-5190-4bcd-ab98-935d9208a4c7">
<articles>
<article ... />
<article ... />
</articles>
</subcategory>
<subcategory id="7077d614-d3de-42d3-851a-b8c9ce0f56df">
<articles>
<article ... />
<article ... />
</articles>
</subcategory>
</subcategories>
</category>
</categories>
But after looking at it, it seems like there are some unnecessary elements here, and that the same data could be shortened to the following:
<?xml version="1.0" encoding="utf-8"?>
<categories>
<category id="cf6cb6bc-8142-4366-9b43-0ae6fce90df2">
<subcategory id="02b95e55-a4f7-4979-b0aa-a97f2c3327b0">
<article ... />
<article ... />
</subcategory>
<subcategory id="5e9f9ef7-5190-4bcd-ab98-935d9208a4c7">
<article ... />
<article ... />
</subcategory>
<subcategory id="7077d614-d3de-42d3-851a-b8c9ce0f56df">
<article ... />
<article ... />
</subcategory>
</category>
</categories>
It seems the file as a whole requires a root node. However, it doesn't appear all subcategories and articles also need a dedicated root nodes. So my shortened version just dumps all the subcategories directly under the category tag, and all the articles directly under the subcategory tag.
Could someone who works more with XML files tell me if there's any reason at all not to use the shortened version above.
I can see no reason to have the extra hierarchy, if there is only one per parent node and they have no attributes or special data of their own. I would just have subcategory
directly under category
and article
directly under subcategory
. I personally like to keep things clean and simple :)
EDIT: I agree with @JimGarrison, if you had other data at that level, which from your example you don't seem to have, then grouping them under one node would have had an advantage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With