DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Oracle: Migrate PDB to Another Database
  • Unveiling the Clever Way: Converting XML to Relational Data
  • Extracting Data From Very Large XML Files With X-definition
  • MDC Logging With MuleSoft Runtime 4.4

Trending

  • Getting Started With Agentic Workflows in Java and Quarkus
  • Multi-Scale Feature Learning in CNN and U-Net Architectures
  • Building a Production-Ready AI Agent in 2026: Beyond the Hello World Demo
  • S3 Vectors: How to Build a RAG Without a Vector Database
  1. DZone
  2. Data Engineering
  3. Databases
  4. Generating and Exporting XML File in Oracle

Generating and Exporting XML File in Oracle

In this post, we will discuss the alternative method for converting a table stored in an Oracle database to XML format and exporting it as an XML file.

By 
Burak KALAYCI user avatar
Burak KALAYCI
·
Nov. 17, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
7.4K Views

Join the DZone community and get the full member experience.

Join For Free

In some cases, there may be a need to write the data stored in traditional databases to XML files. Iin Oracale, there are some XML functions for this subject, however, in this post, we'll discuss the alternative method for converting a table stored in an Oracle database to XML format within an Oracle database and exporting it as an XML file, without using Oracle's XML functions.


You can use that method in stored procedures in Oracle. Assume we have a table called Customer, which has the following columns:

PLSQL
 
CREATE TABLE customer
(
  id         NUMBER,
  first_name VARCHAR2(100),
  last_name  VARCHAR2(100),
  birth_date DATE,
  gender     VARCHAR2(1)
)


Customer table

First of all, we must convert the data of the table to XML format. We should create a string by concatenating the table columns between tags in XML format in SQL query. We can write this formatted data into another table for storage and export.

Here is an example:

PLSQL
 
CREATE TABLE customer_xml
(
  created_date DATE,
  row_num      NUMBER,
  xml_data     VARCHAR2(4000)
);

    INSERT INTO customer_xml
    (created_date,
     row_num,
     xml_data)
    SELECT c.created_date,
           c.row_num,
           CASE
             WHEN c.row_num = 1 THEN
              '<?xml version="1.0" encoding="UTF-8"?><root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
             ELSE
              ''
           END ||
           '<id>' || TO_CHAR(id) || '</id>' ||
           '<first_name>' || first_name || '</first_name>' ||
           '<last_name>' || last_name || '</last_name>' ||
           '<birth_date>' || TO_CHAR(birth_date, 'YYYY-MM-DD') || '</birth_date>' ||
           '<gender>' || gender || '</gender>' ||
           CASE
             WHEN c.row_num = rc.row_count THEN
              '</root>'
             ELSE
              ''
           END AS xml_data
      FROM (SELECT TRUNC(SYSDATE) AS created_date,
                   c.*,
                   ROW_NUMBER() OVER(ORDER BY c.id) AS row_num
              FROM customer c) c
 LEFT JOIN (SELECT TRUNC(SYSDATE) AS created_date,
                   COUNT(*) AS row_count
              FROM customer c) rc
        ON c.created_date = rc.created_date;

COMMIT;


After the XML formatted data is inserted into the table, we can export the file with XML extension. To do that we can use the UTL_FILE package in Oracle. To export the data from the customer_xml table, we can use UTF_FILE.fopen_nchar function. To create an XML file, it is sufficient to add the ".xml" extension to the end of the file name parameter.

After that, the XML file will be created at the location specified in the UTL_FILE function:

PLSQL
 
BEGIN
    fo := UTL_FILE.fopen_nchar (location, filename || '.xml', 'w', 32767);

    FOR c1 IN (SELECT xml_data
                 FROM customer_xml
             ORDER BY row_num)
    LOOP
        UTL_FILE.put_line_nchar (fo, C1.xml_data);
    END LOOP;

    UTL_FILE.fclose (fo);
END;


This is it! What do you think? Please share in the comments below.

XML Database

Opinions expressed by DZone contributors are their own.

Related

  • Oracle: Migrate PDB to Another Database
  • Unveiling the Clever Way: Converting XML to Relational Data
  • Extracting Data From Very Large XML Files With X-definition
  • MDC Logging With MuleSoft Runtime 4.4

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook