Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any Advantage to Giving All Collections a Separate Root Node in XML Documents?

Tags:

xml

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.

like image 214
Jonathan Wood Avatar asked Nov 03 '22 19:11

Jonathan Wood


1 Answers

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.

like image 140
Tamar Avatar answered Nov 15 '22 10:11

Tamar