XML generation with Oracle ADF

Source: GitHub

XML generation is something we use quite extensively in ADF products - be it for PDF generation, or for printing labels on a Zebra printer or for sending data to an external system. And ADF BC does have an out-of-the-box support for generating XML documents straight out of its view objects, via its writeXML API.

However, there are times when we need to stretch the XML documents - maybe create a custom XML, or import a custom node inside a ADF BC generated XML. This article intends to provide a working solution for these extra bits, along with a few tweaks for the standard process.

This article will cover 3 use-cases:
1. Generate a standard XML from ADF BC, with node names of our choice.
2. Generate a custom XML from an XSD schema.
3. Combine the above two XMLs into a composite XML document.

The sample application uses Departments and Employees tables from HR schema. To keep the size of the XMLs small, the VOs have been tuned to return only 5 records.

The first use-case can be solved by using writeXML API from view objects.

The XML generated will have the below structure:

In order to achieve the custom names of the xml nodes, we need to set up XML_ROW_ELEMENT and XML_ELEMENT non-translatable properties for the view object, and XML_ELEMENT properties for the view object's attributes.

Important Note: For child accessors (e.g. Departments > Employees), merely setting translatable properties will not show the custom names. You also need to set the root name as employees at the view link Relationship tab.

When you edit the accessor name in the view link, double check whether the same has been updated on the parent view object (refractoring sometimes goes wrong). If not then you need to manually edit the xml metadata.

That is all we need to configure for generating the standard XML.

The second use-case involves the following steps:
a) Generate XSD from a sample XML document (refer to sample/sample.xml from GitHub repository).
b) Right click on XSD to generate JAXB Content Model (POJOs) from XSD.
c) Populate the data model and use Java marshaller to generate the XML structure.

Once these two steps are done, solving the third use-case is quite simple. We need to get the Element Nodes generated from the above two processes and join them under a root node.

This is the final composite XML:

Cheers! And a merry christmas and happy holidays!