Writing complex XML
This is a very useful recipe for building complex XML structures containing many looping elements and deep hierarchies, and once the principles are understood, it is simple and quick to implement. If you use XML frequently, then we hope that this will become one of your staple recipes.
To make this exercise more understandable, it is necessary to understand a little about the method prior to using it.
Understanding the XML structure
The XML structure we are aiming to create is shown in the following screenshot:
As you can see, a customer can have many orders, and an order can have many order items.
Node
Note
I am using the term node to describe an XML tag that contains one or more other tags or nodes. For example a customer node may contain many order nodes.
Method
We will build a three-tier XML structure building the hierarchy one level at a time:
First, we build the customer node.
Then, we build an orders node that contains many individual order nodes and add them as a child of...