Should XML elements group parents?

Which agreement would be preferable and why (including some of the pros and cons of one of them)?

It:

<company>
    <employees>
        <employee />
        <employee />
        <employee />
    </employees>
    <buildings>
        <building />
        <building />
    </building>
</company>

or that:

<company>
    <employee />
    <employee />
    <employee />
    <building />
    <building />
</company>
+3
source share
5 answers

There is no semantic reason for explicitly representing a collection through a specific element. As a result, both documents have the same meaning - they represent a company with all its employees and buildings.

However, the first example has additional advantages:

  • it is more readable and can benefit from the presentation in editors.
  • you can create a more rigorous scheme
  • easier to serialize into strongly typed collections
  • ,

, (, , ):

  • /
  • , XML.
+1

, XML , . XML- .

, , . Company, Employees, Employee.

, Company , "Employee", "Employee2", "Employee3" - . , .

+2

, ? ( ),

, , .

+1

... , , ?

( <employees> <buildings>) .

XPath, /company/employees/employee vs. /company/employee.

, 6 , .

0

, , .

# :

// Top Example:
public class Company
{
  public Employee[] employees;
  public Building[] buildings;
}

// Vs. Bottom Example:
public Things[] employeesAndBuildings;

, - . , .

The second example is shorter and depending on what the format is reading, the reader can just as easily separate the two elements. However, for readability, the format may become more unreadable if everything goes in a different order:

<company>
    <employee />
    <building />
    <employee />
    <building />
    <employee />
</company>

I would choose the first format.

0
source

Source: https://habr.com/ru/post/1725884/


All Articles