Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 E-mail Us
 Oracle Articles
New Oracle Articles

 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog

 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Oracle Support

 SQL Tuning

 Oracle UNIX
 Oracle Linux
 Remote s
 Remote plans
 Application Server

 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S


 Consulting Staff
 Consulting Prices
 Help Wanted!


 Oracle Posters
 Oracle Books

 Oracle Scripts

Don Burleson Blog 







How to Format Oracle output with HTML/XML

Oracle tips by Burleson
January 15, 2015

Question:  How do I get a dynamic format output from Oracle?  I want to write an Oracle PL/SQL that provides XML and/or HTML formatted data dynamically.  The original of this was a "generic" way of reading the output of a stored procedure for development like the SQL Server TSQL, which formats output automatically.

Answer by Steve Karam: 

Oracle SQL can be embedded in almost any popular procedural language, and Oracle PL/SQL is quite robust, allowing embedded Java. 

Oracle's XML Publisher product has callable procedures that can retrieve Oracle data and create reports with graphs and images, and then format, mail, fax, print, or FTP them to a remote device.

For formatting Oracle output we have the dbms_xmlgen PL/SQL package. This package generates XML "on the fly" using any Oracle SQL query you want.  The dbms_xmlgen package is extremely easy to use from either the SQL prompt or in code, as it's just a simple query.  See here for details on output formatting with the dbms_xmlgen package.


Generating formatted XML From Oracle

Take this standard query as a sample:

select employee_id, first_name, last_name, phone_number 
from employees where rownum

----------- -------------------- ------------------       ------------        100 Steven               King                      515.123.4567        101 Neena                Kochhar                   515.123.4568        102 Lex                  De Haan                   515.123.4569        103 Alexander            Hunold                    590.423.4567        104 Bruce                Ernst                     590.423.4568

We get our standard output, but with no formatting. But it's easy to transform this Oracle output into properly formatted XML.  All we do is change the SQL to embed the requested columns into a call to the dbms_xmlgen.getxml procedure:

set pages 0
set linesize 150
set long 9999999
set head off

SQL> select dbms_xmlgen.getxml('select employee_id, first_name,
2 last_name, phone_number from employees where rownum < 6') xml
3 from dual;

Beautiful! Fully compliant XML that can be easily integrated into any application, with ROWSET and ROW tags in place to identify nodes, and tags for each column you pulled out of the database.

All we had to do was wrap the query in the DBMS_XMLGEN.GETXML function call, selected from DUAL. The query remained exactly the same.

Advanced formatting of Oracle data

Let's take this easy formatting one step further and show how Oracle automatically formats hierarchical reports. Most XML has subnodes for each main node, allowing a formatted hierarchy. For example, assume we wanted to pull XML for every department, and a subnode for every employee under it? For this formatting, we use the CURSOR function!

SQL> select department_id, department_name,
2 cursor(select first_name, last_name
3 from employees e
4 where e.department_id = d.department_id) emp_row
5 from departments d 6* where rownum < 4

The results don't look too impressive at the SQL prompt. However, watch as we surround it with a call to DBMS_XMLGEN.GETXML:

select dbms_xmlgen.getxml('
select department_id, department_name,
cursor(select first_name, last_name
from employees e
where e.department_id = d.department_id) emp_row
from departments d
where rownum < 4
* ') from dual;

Note that I didn't change the query in any way. But check out the results! We have each DEPARTMENT as a ROW tag, and the cursor we created gives us an EMP_ROW node containing recurring EMP_ROW_ROW nodes.

Conclusions on dbms_xmlgen

The dbms_xmlgen procedure can be extremely useful for quick retrieval of Oracle records, formatted for web browser display. With these formatting procedures you can display the output of any query directly to the screen, and you have an easy XML display program. The best part comes with easily formatting Oracle reports. XML Publisher is made to accept XML that looks just like this and form extremely detailed reports using templates made in Microsoft Word.

With queries such as these and XML Publisher you can have a full reporting suite that easily pulls data, forms it into a PDF, DOC, XLS, or HTML report, and distributes it anywhere you would like it to go.

Learn more about Oracle XML formatting:

See our notes for more details on easily formatting Oracle output:


If you like Oracle tuning, you might enjoy my book "Oracle Tuning: The Definitive Reference", with 950 pages of tuning tips and scripts. 

You can buy it direct from the publisher for 30%-off and get instant access to the code depot of Oracle tuning scripts.



Oracle Training at Sea
oracle dba poster

Follow us on Twitter 
Oracle performance tuning software 
Oracle Linux poster


Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


Copyright © 1996 -  2016

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.