2

I'm designing an XML response for a consuming application to take in, and I'm weighing two different designs. Currently I have a design as follows (where the Product element can repeat):

<Response>
    <AccountType>
    <ProductType>
    <Products>
       <Product>
           <ProductID />
           <ProductStatus />
           <ProductName />
       </Product>
       <Product>
           <ProductID />
           <ProductStatus />
           <ProductName />
       </Product>
    </Products>
</Response>

An alternate design is:

<Response>
    <AccountType>
    <ProductType>
    <Product>
        <ProductID />
        <ProductStatus />
        <ProductName />
    </Product>
    <Product>
        <ProductID />
        <ProductStatus />
        <ProductName />
    </Product>
</Response>

In the first design I have a master element called Products, under which each Product element exists and can repeat within Products. The second design is a little flatter - where Product exists under the main Response node.

Which one of the two is more optimal for a consuming application to read from and process?

Ryan
  • 1,105
  • 1
  • 10
  • 17

2 Answers2

15

The one that wraps all of the products into a single element. You can treat it as a collection in most programming languages that offer serialization/deserialization.

See XML Serialization of Arrays and Collections

Arrays and collections can be serialized to XML. The standard action when using the default serializer is for the name of the collection property to be added to the XML, with a contained element for each item named according to the items' data types...

gnat
  • 21,442
  • 29
  • 112
  • 288
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
2

If you're processing it with XPath or XSLT it doesn't make much difference. If you're using a programming language less well adapted to XML, the wrapper element probably makes life easier.

Michael Kay
  • 3,360
  • 1
  • 15
  • 13