Nested Structures in XML
XML uses tree structures to define data items as well as the relationships between them. For XML to be correctly formed, elements must be correctly structured. Each element in XML typically includes an opening tag, a closing tag and some content between them. The content can also include other XML elements, which creates nested structures. The element containing other elements is the parent, while the contained elements are the children.
-
Parent Elements
-
Nested structures in XML include parent elements and child elements. The following example markup code demonstrates a parent element with some child elements inside it:
<country name="France">
<city>Paris</city>
<city>Bordeaux</city>
</country>
In this case the "country" element is the parent element. If this structure appears first within an XML document, the "country" element will also be the document's root element. Since the "country" element is the parent, its closing tag must appear outside the closing tags for the child elements, otherwise the XML will not be correctly formed.
Child Elements
-
Child elements appear within parent elements in nested XML structures. Child elements can also be parent elements if they contain further child elements inside them. For a sample of XML data to be well-formed, the closing tags of a child element must appear before the closing tag for its parent element. Child elements also have an opening and closing tag in most cases, although they can sometimes be self-closing as in the following sample markup:
<landmark name="Eiffel Tower"/>
You don't need to include a closing "landmark" tag in this case, as the element is closing itself.
-
Nesting
-
XML nesting errors are common. In general, the last element opened should be the first one closed. The following sample markup code demonstrates incorrectly nested elements:
<country name="France">
<city>
Paris
</country>
</city>
The nesting in this case is incorrect because the "country" element closing tag appears before the "city" element closing tag. In long, complex XML documents and data stores, the nesting can quickly become confusing, often leading to error. Using an XML editor that highlights and indents the nested structures can help to avoid mistakes.
Element Number
-
Depending on the rules for any particular application, which developers can define using XML Schema Definitions, XML can contain multiple parent and child elements. A single parent element can contain many child elements, while the document as a whole can contain multiple parent elements, as in the following sample markup code:
<places>
<country name="Spain">
<city>Madrid</city>
</country>
<country name="Germany">
<city>Berlin</city>
<city>Munich</city>
</country>
</places>
In this case, the "places" element is the root element within the document. The data can contain multiple "country" elements, which are child elements of the parent "places" element. Each "country" element can in turn contain multiple child "city" elements.
-
References
Resources
- Photo Credit Hemera Technologies/Photos.com/Getty Images