Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 15 down vote accepted

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...

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.