AWS Database Blog
Migrate Oracle’s XMLDOM package functions to Amazon Aurora PostgreSQL using JSON format
Migrating from Oracle to Amazon Aurora PostgreSQL-Compatible Edition or Amazon Relational Database Service (Amazon RDS) for PostgreSQL may be challenging if the application is using the DBMS_XMLDOM package to access XML type objects. This is because the DBMS_XMLDOM package isn’t supported in Aurora PostgreSQL or Amazon RDS for PostgreSQL. Moreover, converting procedures to Aurora PostgreSQL or Amazon RDS for PostgreSQL could be a cumbersome and time-consuming process depending on the amount of stored procedures dependent on the DBMS_XMLDOM package.
In this post, we discuss a solution that uses the JSON data type instead of XML to migrate stored procedures from Oracle to PostgreSQL and reduce migration efforts. We chose JSON because JSON is the native format for data in JavaScript, and PostgreSQL has extensive support of JSON compared to XML.
Overview of solution
The DBMS_XMLDOM package in Oracle is used to access XML type objects and implements the Document Object Model (DOM), an application programming interface for HTML and XML documents.
In this solution, we create the DBMS_XMLDOM schema and wrapper package functions equivalent to Oracle in a PostgreSQL database using the PLV8 extension. PLV8 is a trusted JavaScript language extension for PostgreSQL that you can use for stored procedures, triggers, and more.
This solution uses the JSON data type instead of XML to convert Oracle’s DBMS_XMLDOM package to PostgreSQL. Therefore, make sure to complete a thorough assessment of the database and its dependent applications before proceeding with this solution and make sure there are no objections or concerns to using the JSON data type.
Prerequisites
Assuming that you have an Oracle and a PostgreSQL databases up and running, proceed with the following steps.
Before getting started, you have to create several database objects in PostgreSQL:
- Create the DBMS_XMLDOM schema in PostgreSQL:
- Install the plv8 extension:
PLV8 is a shared library that provides a PostgreSQL procedural language powered by the V8 JavaScript engine.
- Create the following wrapper functions under the DBMS_XMLDOM schema:
- Wrapper function
dbms_xmldom.addelement
(this function adds new element into the document): - Wrapper function
dbms_xmldom.getattributes
(this function retrieves the attributes of the node): - Wrapper function
dbms_xmldom.getchildrenbytagname
(returns the children of the document): - Wrapper function
dbms_xmldom.getelementbytagname
(returns the element in the subtree by the tag name): - Wrapper function
dbms_xmldom.getlength
(retrieves the number of items in the map): - Wrapper function
dbms_xmldom.getnodename
(retrieves the name of the node): - Wrapper function
dbms_xmldom.getnodevalue
(retrieves the value of the node): - Wrapper function
dbms_xmldom.item
(retrieves the item given the index in the map): - Wrapper function
dbms_xmldom.setattribute
(sets the attribute specified by name; the function is overloaded with TEXT, INTEGER, and DOUBLE PRECISION data types):
- Wrapper function
Example 1: Compare number of nodes
Let’s use the DBMS_XMLDOM.GETLENGTH function to return the number of nodes in the list and compare the output between Oracle and PostgreSQL.
Run the following code block in Oracle:
You get the following output.
In order to call the DBMS_XMLDOM.GETLENGTH function in PostgreSQL, we have to first convert the XML to JSON format.
XML | JSON |
Run the following code in PostgreSQL:
You get the following output.
Example 2: Generate an XML document
Let’s create a stored procedure to generate an XML document using the DBMS_XMLDOM package function.
This procedure covers the following DBMS_XMLDOM package functions:
dbms_xmldom.getdocumentelement
dbms_xmldom.makeNode
dbms_xmldom.makeNode
dbms_xmldom.createElement
dbms_xmldom.appendChild
dbms_xmldom.setattribute
The following Oracle procedure code generates the XML document:
The following is the equivalent PostgreSQL procedure code:
Test case 1
In the following Oracle call statement, xmlParams is NULL:
The following screenshot shows our output.
The following is the equivalent PostgreSQL call statement:
You get the following output.
Test case 2
In the following Oracle call statement, xmlParams contains a value:
The following screenshot shows our output.
The following code is the equivalent PostgreSQL call statement:
We get the following output.
Example 3: Process the XML document
Let’s create another stored procedure to process the XML document. This procedure accepts the XML document created by the Generate_XML
procedure as the input parameter, reads all the items, and stores them in the TRANSACTION_DETAILS
table.
This procedure covers the following DBMS_XMLDOM package functions:
dbms_xmldom.getattributes
dbms_xmldom.getnodevalue
dbms_xmldom.item
dbms_xmldom.getelementbytagname
dbms_xmldom.getlength
Let’s create the TRANSACTION_DETAILS table in our databases.
Oracle | PostgreSQL |
The following Oracle code creates the Process_XML
procedure:
The following procedure code is the PostgreSQL equivalent:
Test case 3
In the following Oracle call statement, xmlParams is NULL:
We get the following output.
The following code is the PostgreSQL equivalent:
We get the following output.
Test case 4
In the following Oracle call statement, xmlParams contains a value:
The following screenshot shows our output.
To verify the table record in Oracle, run the following query:
You get the following output.
The following call statement is the PostgreSQL equivalent:
We get the following output.
Verify the table record in PostgreSQL with the following query:
You get the following output.
Conclusion
In this post, we showed you a solution to migrate Oracle’s DBMS_XMLDOM package functions to Aurora PostgreSQL or Amazon RDS for PostgreSQL using the JSON data type.
If you have any questions or comments, share your thoughts in the comment section.
About the Authors
Jitendra Kumar is a Lead Database Migration Consultant with AWS Professional Services. He helps customers migrate and modernize workloads in the AWS Cloud, with a special focus on modern application architectures and development best practices.
Sai Parthasaradhi is a Database Migration Specialist with AWS Professional Services. He works closely with customers to help them migrate and modernize their databases on AWS.