1. Technology

XML Tutorial - Adding Child Elements

Understanding the Basic Rules of XML

By

The first part of this series, Understanding the Basic Rules of XML, discussed the need for a well-formed XML document. If the structure of the file does not conform to the basic rules of XML, the parser of some browsers may not be able to see the data. To review that information, check out XML Tutorial - The Declaration and Root Element.

XML Rule #3 – Add Child Elements

When creating an effective file following the rules of XML, child elements organize the information. A root element is a container for all other elements. While you must have a root to create a viable XML file, it is the child elements that add functionality to the page. Consider the root element a filing cabinet and child elements are the drawers to that cabinet. You may add as many child elements to the root as necessary.

‹?xml version= “1.0”›
‹message›
    ‹Dick› - child element opening tag
    ‹/Dick›- child element closing tag
    ‹Jane› - child element opening tag
   ‹/Jane› - child element closing tag
‹/message›

‹Dick› is a child of the root ‹message›. Notice that child elements follow the same format as the root element. They must have matching opening and closing tags. Upper and lower case matters in XML, you cannot use upper case letters for the opening tag and lower case for the closing. Since XML has no predefined elements, the parser will see tags that do not match as different units. ‹JANE› and ‹/jane› will  appear different in XML.

All Elements Must be Properly Nested

This is a major difference between HTML and XML. In HTML, you could put two tags together like this:

‹b›‹i›and close them ‹/b›‹/i›

This will work even though technically ‹i› is nested inside ‹b›. XML demands order, not chaos. Close the elements in the same order that you open them. You would not close ‹message› before ‹Dick›. If you think about the structure of XML, it would make no sense to close the root before the child element. You do not lock the cabinet before shutting the drawer.

In the next article, it will be time to add data strings and attributes, so that XML communicates with the viewer of the web page.

  1. About.com
  2. Technology
  3. Web Design / HTML
  4. XML
  5. Beginning XML
  6. Rules of XML - XML Tutorial - Adding Child Elements

©2014 About.com. All rights reserved.