tags:

views:

150

answers:

4

I'm creating an application that will store a hierarchical collection of items in an XML file and I'm wondering about the industry standard for storing collections in XML. Which of the following two formats is preferred? (If there is another option I'm not seeing, please advise.)

Option A

<School>
    <Student Name="Jack" />
    <Student Name="Jill" />
    <Class Name="English 101" />
    <Class Name="Math 101" />
</School>

Option B

<School>
    <Students>
        <Student Name="Jack" />
        <Student Name="Jill" />
    </Students>
    <Classes>
        <Class Name="English 101" />
        <Class Name="Math 101" />
    </Classes>
</School>
+2  A: 

I'm no XML expert, but I find Option B to be more human readable, and I think it's just as machine readable as Option A. I believe that XML is designed to be both human and machine readable, so I would go for Option B myself.


I just realized something else after Ryan Farley's post. If the Students or Classes section becomes too big and must be moved to another XML file, it seems like it would be easier to copy the node and create a new XML file out of that node with Option B.

Thomas Owens
+2  A: 

Definitely - Option B.

I wouldn't mix students and classes in the XML just the same way that I wouldn't mix students and classes in the same table in a database.

Ryan Farley
+1  A: 

Option B, absolutely. When there's a logical grouping of similar items, it should have a parent item. That way, my parser won't have to step through all 500 student records checking to see if there are class records mixed in.

ceejayoz
+1  A: 

Another compelling reason to use option B is error checking. If the original file is modified outside an XML application, or if no XSD schema is applied, there could be the case where you have an uneven number of students and classes.

At least if you have the students and classes grouped together, you will easily be able to tell if each record is complete, independently of any other record.

Jason Z