Menu
Nesting XML Elements by Kevin Howard Goldberg

Oftentimes when creating your XML document, you'll want to break down your data into smaller pieces. In XML, you can create child elements of child elements of child elements, etc. The ability to nest multiple levels of child elements enables you to identify and work with individual parts of your data and establish a hierarchical relationship between these individual parts.

To nest elements:1. Create the opening tag of the outer element.
2. Type <inner>, where inner is the name of the first individual chunk of data; the first child element.
3. Create the content of the <inner> element, if any.
4. Then, type </inner> matching the word chosen in Step 2.
5. Repeat Steps 2–4 as desired.
6. Finally, create the closing tag of the outer element.

Tips

Figure 1.14 To make sure your tags are correctly nested, connect each set with a line. None of your sets of tags should overlap any other set; each inner set should be completely enclosed within its next outer set.

It is essential that each element be completely enclosed in another. In other words, you may not write the closing tag for the outer element until the inner element is closed. Otherwise, the document will not be considered well-formed, and will generate an error in the XML processor (Figure 1.14).

<?xml version="1.0"?>
<ancient_wonders>
 <wonder>
 <name>Colossus of Rhodes</name>
 <location>Rhodes, Greece</location>
 <height units="feet">107</height>
 </wonder>
</ancient_wonders>
Figure 1.15 Now the wonder element is nested as a child of the ancient_wonders element, and name, location and height are nested as child elements of the wonder element.

You can nest as many levels of elements as you like (Figure 1.15).

When nesting elements, best practices suggest that you indent the child element. This enables you to easily see parent, child, and sibling relationships. Most XML editors will automatically do this for you.

About the Author

Kevin Howard Goldberg has been working with computers since 1976 when he taught himself BASIC on his elementary school's PDP 11/70. Since then, Kevin's career has included management consulting, lead software development and in his current capacity, he runs technology operations for a world-class Internet Strategy, Marketing and Development company. Kevin holds a bachelor's degree in Economics and Entrepreneurial Management from the Wharton School of Business at the University of Pennsylvania, and is a candidate for a master's degree in Computer Science at the University of California, Los Angeles.

Reader J. Hatch says, "This book is a great way to get started if you have not done a lot of HTML and are looking for a way to get your feet wet with XML and it's interactions with HTML. It does a great job of step-by-step leading you through most of the basic concepts used in XML. If you don't know much about XML and want to get started and need a quick reference for figuring out what is going on in an XML file, this is a great starting point. It is compact and the approach is to explain something minimally and then show a example. There are example files to download that the book references. To get the most out of this book, you will have to download the examples."

Learn more about XML: Visual Quickstart Guide at amazon.com


Learn more at amazon.com