Advanced XML output for complex XML structures
The XML output format we used in the last example was pretty straightforward. Every piece of data is contained within its own XML element; however, it is very common for XML files to be more complex, with subelements being repeated within a parent element, and data items being held in XML attributes rather than in elements.
In this example, we will produce an XML file that contains data about customer orders that have been dispatched. This file will contain the following information:
The order ID
The order-line ID
The product SKU for each line
The quantity of each dispatched SKU
The dispatch date (in the format
yyyy-MM-dd hh:mm
)The courier tracking ID (so that customers can track their order)
The XML format we need to adhere to is as follows:
<?xml version="1.0" encoding="UTF-8"?> <DISPATCH_DOCKET> <ORDER ID="1000"> <ORDER_LINE ID="1" SKU="123456789" QUANTITY="1" DISPATCHED_DATE="2011-01-01 12:00" TRACKING_ID="ABC12345"/> <...