How to translate SQL data to XML in Oracle?


Problem Statement:
You need to convert data stored in traditional form in your database to XML document.

Solution: Oracle have many different functions for converting data to XML format. Some of the functions Oracle provides for converting relational data to XML are the SYS_XMLGEN, DBMS_XMLGEN, and XMLELEMENT functions.

We will be using SYS_XMLGEN to convert rows to XML.The SYS_XMLGEN function returns a block of XML based on a row/s or row like expression. SYS_XMLGEN can take a literal value, a single column, or a user defined type as input parameter.

We will be creating XML from students data for an student record.

We will begin by creating a mapping to students table that can be used by SYS_XMLGEN.

Example

CREATE OR REPLACE TYPE student_map AS OBJECT ("@studentno" NUMBER(10),    fname  VARCHAR2(50),    lname  VARCHAR2(50),    phone  VARCHAR2(50) );

We now use the student_map type to input data stored relationally for XML processing.

Example

CREATE OR REPLACE PROCEDURE student_xml_data (stu_id IN number) AS   xml_stu XMLTYPE; BEGIN   SELECT   SYS_XMLGEN     (student_map       (student_id ,        first_name ,        last_name ,        phone_number )      ) INTO xml_stu   FROM students   WHERE student_id = stu_id;   dbms_output.put_line(xml_stu.getclobval()); END;

In the above procedure we used student_map to dynamically cast the results from the SELECT statement into the user defined datatype. The result of the SYS_XMLGEN call is then directed to the student_map variable by the INTO clause. We finally, used to getclobval to view the raw XML.

Code: Call the procedure for student id 100 to get the XML.

Example

BEGIN student_xml_data(100); END;

Output

<?xml version="1.0"?> <ROW studentno="100">  <FNAME>SMITH</FNAME>  <LNAME>JAMES</LNAME>  <PHONE>111.111.1245</PHONE> </ROW>

One limitation of the SYS_XMLGEN function is that it cannot accept multiple columns or even the result of a SELECT * … statement.

Data Preparation: Data used for the problem is shown below. The data is completely made up for demonstration purpose.

Example

DROP TABLE students;
COMMIT;

CREATE TABLE students
    ( student_id     NUMBER(6)
    , first_name     VARCHAR2(20)
    , last_name      VARCHAR2(25) 
    , email          VARCHAR2(40) 
    , phone_number   VARCHAR2(20)
    , join_date      DATE 
    , class_id       VARCHAR2(20)  
    , fees           NUMBER(8,2)
    , professor_id   NUMBER(6)
    , department_id  NUMBER(4) 
    ) ;

Example

CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ;
INSERT INTO students VALUES (100,'SMITH','JAMES','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL);
INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90);
INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90);
INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60);
INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60);
INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60);
INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60);
INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60);
INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100);
INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100);
INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100);
INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100);
INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100);
INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100);

COMMIT;

Updated on: 04-Dec-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements