Export ViewObject dataset to XML, Generate customized XML file using ViewObject in Oracle ADF

Sharing is Caring

Hello All,
this post is about exporting a viewObject data to an XML document.
Sometimes we need to generate XML document with the same data in ViewObject, for this ADF provides a facility to directly export viewobject data to an XML document using ViewObjectImpl class

See the steps to generate XML for a ViewObject

    • Create a Fusion Web application and model using HR schema (Departments & Employees) table (master-detail relation using viewLink)

 

 

    • Now to generate XML, i have used writeXML method of ViewObjectImpl class, it produce XML using two parameters

from oracle docs-
writeXML(int depthCount, long options)
here depthCount – no. of ViewLink levels that should be traversed to produce XML
options- how many rows you want to export, It can be set any of flags given below
XMLInterface.XML_OPT_ALL_ROWS
Includes all rows in the view object’s row set in the XML.

XMLInterface.XML_OPT_LIMIT_RANGE
Includes only the rows in the current range in the XML.

    • Created a method in DepartmentsVOImpl class to export data to XML, added it to client Interface

    /**Method to generate XML from ViewObject data
     * @param level
     * @return
     */
    public String writeVoToXml(int level) {
        FileOutputStream out;
        ByteArrayOutputStream opStream = new ByteArrayOutputStream();
        try {
            // Generating XML for All rows and adding it to Output Stream
            ((XMLNode) this.writeXML(level, XMLInterface.XML_OPT_ALL_ROWS)).print(opStream);
            System.out.println(opStream);
            // Creating a XML document in D Drive
            out = new FileOutputStream("D://Departments.xml");
            out.close();

        } catch (Exception e) {
    System.out.println(e.getMessage());
        }

        return opStream.toString();

    }

 

 

    • Now run application module to execute method and see generated XML on the console
BC4J Tester

 

ViewObject XML

 

    • I have created a simple page with an af:codeEditor to show generated XML, the button to generate XML and a spinner to pass depth level of view link accessor

In case depth level is ‘0’, it export data only for Departments viewObject

Change level to ‘1’ , Now it generate XML for Departments –> Employess relation

 

    • Now to how to customize XML? How to change default tags for attribute names and rows?
    • To change attribute label (tag in XML)- Suppose I have to change DepartmentName to Name in XML
    • Add an attribute level custom property named XML_ELEMENT to a value AnyOtherName to change the XML element name used for that attribute

 

 

    • To change Row label (tag in XML)- Suppose I have to change DepartmentsVORow to DepartmentLine in XML
    • Add the ViewObject level custom property named XML_ROW_ELEMENT to a value AnyOtherName to change the XML element name used for that Row

 

 

  • Now Run and see generated XML –
Export ViewObject

Cheers 🙂 Happy Learning

Related Posts

I’m an Oracle ACE, Blogger, Reviewer,
Technical Lead working on Oracle ADF

Leave a Reply

Your email address will not be published. Required fields are marked *