How to Format Oracle output with HTML/XML
Oracle Database Tips by Burleson Consulting
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
Generating formatted XML From Oracle
Take this standard query as a sample:
select employee_id, first_name, last_name,
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
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
set pages 0
set linesize 150
set long 9999999
set head off
SQL> select dbms_xmlgen.getxml('select
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,
cursor(select first_name, last_name
3 from employees e
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:
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
You can buy it direct from the publisher for 30%-off and get instant
access to the code depot of Oracle tuning scripts.