You can use the DBMS_XMLGEN built-in package to export data from a database in XML format for data exchange or integration with external systems. This package converts the result of any SQL query into a Character Large Object (CLOB) that contains XML data, which simplifies the data format conversion process.
Overview
The DBMS_XMLGEN built-in package provides functions and procedures to generate standard XML documents from SQL query results. Its core features include the following:
Direct conversion: You can use the
getXMLfunction to convert the result of an SQL query into the XML format using a single line of code.Step-by-step conversion using contexts: You can also create a query context using
newContext, generate XML based on this context usinggetXML, and then release the resources usingcloseContext. This method provides more flexibility for processing complex or step-by-step logic.Escape special characters: The
convertfunction processes special characters in XML, such as<,>, and&, to ensure that the generated XML document is correctly formatted.
Prerequisites
The following versions of PolarDB for PostgreSQL (Compatible with Oracle) are supported:
Oracle syntax compatibility 2.0 (minor engine version 2.0.14.17.35.0 or later)
You can view the minor engine version in the console or by running the SHOW polardb_version; statement. If the minor engine version is not supported, upgrade the minor engine version.
Notes
When you use DBMS_XMLGEN, there are a few incompatibilities between PolarDB for PostgreSQL (Compatible with Oracle) and Oracle:
Case sensitivity of column names: In PolarDB, column names that are not enclosed in double quotation marks are lowercase by default, whereas in Oracle, they are uppercase by default. As a result, the XML tags generated by
getXMLhave different cases.If the downstream system is case-insensitive or can adapt to lowercase tags, no operation is required.
If the downstream system requires uppercase tags, such as
/ROWSET/ROW/COLUMN_NAME, set thepolar_dbms_xmlgen_upper_column_type_nameparameter to ON in the console.
Column aliases: If the query executed by the
getXMLfunction involves a function invocation or a constant, such asSELECT DBMS_XmlGen.getXML('SELECT my_function(1) FROM dual') FROM dual;:Oracle: The column name in the returned XML is the Unicode-formatted string
MY_FUNCTION_x0028_1_x0029_.PolarDB: The column name in the returned XML is
my_function.
In this case, we recommend that you add an alias to the function invocation, such as
SELECT DBMS_XmlGen.getXML('SELECT my_function(1) AS column_name FROM dual') FROM dual;. When a column has an alias, the behavior of PolarDB is consistent with that of Oracle.Scenario with the
no_data_foundexception: If an SQL statement returns 0 rows, the function returns NULL directly, which is compatible with Oracle. However, if you invoke this function within thegetXMLfunction, such asSELECT DBMS_XmlGen.getXML('SELECT func_return_null FROM dual') FROM dual;, the result may be incompatible with Oracle. We recommend that you do not use this function in this scenario.CREATE OR REPLACE FUNCTION func_return_null RETURN int AS result int := 1; BEGIN -- Always returns 0 rows, triggers the no_data_found exception, and directly returns NULL SELECT 1 INTO result FROM dual WHERE 1 = 0; RETURN result; END; /
Subprogram overview
Subprogram | Description |
| Escapes XML characters. The input parameter is of the |
| Escapes XML characters. The input parameter is of the |
| Obtains an XML document based on the specified SQL statement. |
| Creates a new context handle based on the specified SQL statement. |
| Obtains an XML document based on the context handle and returns a |
| Obtains an XML document based on the context handle and fills it into the existing |
| Closes the context resource. |
Get started
Prepare data
The examples in the following sections are based on the employees table schema and data. To test the examples, run the following SQL statements in your cluster to create and populate the table.
-- Create the employees table
CREATE TABLE employees (
"EMP_id" NUMBER,
name VARCHAR2(50),
dept VARCHAR2(30),
"SALARY" NUMBER
);
-- Insert test data
INSERT INTO employees VALUES (1, 'Zhang San', 'Technical Department', 15000);
INSERT INTO employees VALUES (2, 'Li Si', 'Finance Department', 12000);
INSERT INTO employees VALUES (3, 'Wang Wu', 'Technical Department', 18000);Generate XML directly from an SQL query
Use the getXML function to directly convert a query to XML.
Function
DBMS_XMLGEN.GETXML (
sqlQuery IN VARCHAR2,
dtdOrSchema IN number := NONE)
RETURN CLOB;Parameters
sqlQuery: The specified SQL statement.dtdOrSchema: This parameter is not supported.
Example
SELECT DBMS_XmlGen.getXML('SELECT * FROM employees') FROM dual;The following result is returned:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<EMP_id>1</EMP_id>
<name>Zhang San</name>
<dept>Technical Department</dept>
<SALARY>15000</SALARY>
</ROW>
<ROW>
<EMP_id>2</EMP_id>
<name>Li Si</name>
<dept>Finance Department</dept>
<SALARY>12000</SALARY>
</ROW>
<ROW>
<EMP_id>3</EMP_id>
<name>Wang Wu</name>
<dept>Technical Department</dept>
<SALARY>18000</SALARY>
</ROW>
</ROWSET>Generate XML based on a context handle
For more complex logic, you can perform the operation in steps: create a context, generate XML, and then close the context.
Create a context: Use the
newContextfunction to create a context handle based on an SQL query.Retrieve XML: Pass the handle to the
getXMLfunction or procedure to retrieve the XML result.Close the context: Use the
closeContextfunction to release the handle and related resources.
Function
-- Create a context handle
DBMS_XMLGEN.NEWCONTEXT (
query IN VARCHAR2)
RETURN ctxHandle;
-- Obtain XML from the handle (returns CLOB)
DBMS_XMLGEN.GETXML (
ctx IN ctxHandle,
dtdOrSchema IN number := NONE)
RETURN CLOB;
-- Obtain XML from the handle (fills into an existing CLOB variable)
DBMS_XMLGEN.GETXML (
ctx IN ctxHandle,
tmpclob IN OUT NCOPY CLOB,
dtdOrSchema IN number := NONE);
-- Close the context
DBMS_XMLGEN.CLOSECONTEXT (
ctx IN ctxHandle);Example
DECLARE
xml_result CLOB;
handle DBMS_XMLGEN.ctxHandle;
BEGIN
handle := DBMS_XMLGEN.NEWCONTEXT('SELECT * FROM employees');
DBMS_OUTPUT.PUT_LINE(handle);
xml_result := DBMS_XMLGEN.GETXML(handle);
DBMS_OUTPUT.PUT_LINE(xml_result);
END;
/
DECLARE
xml_result CLOB := '<dummy></dummy>';
handle DBMS_XMLGEN.ctxHandle;
BEGIN
handle := DBMS_XMLGEN.NEWCONTEXT('SELECT * FROM employees');
DBMS_OUTPUT.PUT_LINE(handle);
DBMS_XMLGEN.GETXML(handle, xml_result);
DBMS_OUTPUT.PUT_LINE(xml_result);
END;
/Escape special XML characters
Use the convert function to encode or decode XML entities in a string to handle special characters.
Original character | Escaped character |
|
|
|
|
|
|
|
|
|
|
Function
-- The input parameter is of the VARCHAR2 type
DBMS_XMLGEN.CONVERT (
xmlData IN VARCHAR2,
flag IN NUMBER := ENTITY_ENCODE)
RETURN VARCHAR2;
-- The input parameter is of the CLOB type
DBMS_XMLGEN.CONVERT (
xmlData IN CLOB,
flag IN NUMBER := ENTITY_ENCODE)
RETURN CLOB;Parameters
xmlData: The string to be processed.flag: The conversion pattern.ENTITY_ENCODEor0(default): Encodes special characters.ENTITY_DECODEor1: Decodes special characters.
Example
-- Escape special characters (default pattern)
SELECT DBMS_XMLGEN.CONVERT('<>&"''') FROM dual;
-- Returns: <>&"'
-- Decode the escaped string back to the original characters
SELECT DBMS_XMLGEN.CONVERT('<>&"'', 1) FROM dual;
-- Returns: <>&"'