472,334 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,334 developers and data experts.

Data Fetch in XML Structure using DBMS_XMLGEN

amitpatel66
2,367 Expert 2GB
In real time applications, there is always a requirement to generate the data selected from database in XML format.

using Oracle, generating XML Structured data becomes easier using the inbuilt package DBMS_XMLGEN. This package can be used to convert the OUTPUT of the SELECT Query in to XML Format.

The below code can be used to generate the SELECT Query OUTPUT in XML Format:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DECLARE
  3. my_context NUMBER := 0;
  4. sample_xml_output VARCHAR2(32767);
  5. data_size NUMBER := 0;
  6. data_chunk NUMBER := 0;
  7. offset NUMBER := 0;
  8.  
  9. BEGIN
  10.  
  11. -- Create a New Context
  12.  
  13. my_context:= DBMS_XMLGEN.NEWCONTEXT ('SELECT * FROM emp order by 1');
  14. DBMS_OUTPUT.PUT_LINE('My New Context ID is:'||my_context);
  15.  
  16. -- Set the Root Element
  17.  
  18. DBMS_XMLGEN.SETROWSETTAG(my_context,'EMP_DETAILS');
  19.  
  20. -- Set the Row Element
  21.  
  22. DBMS_XMLGEN.SETROWTAG(my_context,'EMP');
  23.  
  24. -- Generate XML Output
  25.  
  26. SELECT DBMS_XMLGEN.GETXML(my_context) INTO sample_xml_output FROM DUAL;
  27. data_chunk := LENGTH(sample_xml_output);
  28. offset := 0;
  29. data_size := 200;
  30.  
  31. -- Print 200 Chunk of data on Screen
  32.  
  33. LOOP
  34. DBMS_OUTPUT.PUT_LINE(SUBSTR(sample_xml_output,offset + 1,data_size));
  35. offset:= offset + data_size;
  36. data_size := LEAST(200,data_chunk - data_size);
  37. data_chunk := data_chunk - data_size;
  38. EXIT WHEN data_chunk <= 0;
  39. END LOOP;
  40.  
  41. -- Close the Context
  42.  
  43. DBMS_XMLGEN.CLOSECONTEXT(my_context);
  44.  
  45. --Exception Block
  46.  
  47. EXCEPTION
  48. WHEN OTHERS THEN
  49. NULL; -- Add your exception handler message here
  50. END;
  51. /
  52.  
  53.  
Let me explain each Functions/Procedures used in the above code:

DBMS_XMLGEN.NEWCONTEXT

This function is used to create a NEW CONTEXT that returns the CONTEXTID. This CONTEXTID will be required when other functions/procedures of DBMS_XMLGEN package is used

DBMS_XMLGEN.SETROWSETTAG

This Procedure is used to SET the ROOT ELEMENT for the XML Data. In the above example, I am setting the ROOT ELEMENT as EMP_DETAILS. You can check the ROOT ELEMENT in the Output posted below which will be set to EMP_DETAILS. The Root Element can be set to anything depending on the type of data it holds.

DBMS_XMLGEN.SETROWTAG

This Procedure is used to SET the ROW TAG for the XML Data. Since the output is Employee details, I am setting the ROW TAG to EMP

DBMS_XMLGEN.GETXML

This Function is used to generate the XML Structured output for the New Context that we have created. While creating a New Context, we passed a SELECT Query as an Input Parameter. So using this Function will Convert the Output of the SELECT Query to XML Format Data using the Corresponding ROW TAG andROWSET TAG that we have defined using SETROWSETTAG and SETROWTAG Procedures as shown above

Check below Sample Output generated by the code posted above:

Expand|Select|Wrap|Line Numbers
  1.  
  2. My New Context ID is:44
  3. <?xml version="1.0"?>
  4. <EMP_DETAILS>
  5.  <EMP>
  6.   <EMPNO>7369</EMPNO>
  7.   <ENAME>SMITH</ENAME>
  8.   <JOB>CLERK</JOB>
  9.   <MGR>7902</MGR>
  10.   <HIREDATE>17-DEC-80</HIREDATE>
  11.   <SAL>800</SAL>
  12.   <DEPTNO>20</DEPTNO>
  13.  </
  14. EMP>
  15.  <EMP>
  16.   <EMPNO>7499</EMPNO>
  17.   <ENAME>ALLEN</ENAME>
  18.   <JOB>SALESMAN</JOB>
  19.   <MGR>7698</MGR>
  20.   <HIREDATE>20-FEB-81</HIREDATE>
  21.   <SAL>1600</SAL>
  22.   <COMM>300</COMM>
  23.   <DEPTNO>30</DEPTNO>
  24.  </EMP>
  25.  <E
  26. MP>
  27.   <EMPNO>7521</EMPNO>
  28.   <ENAME>WARD</ENAME>
  29.   <JOB>SALESMAN</JOB>
  30.   <MGR>7698</MGR>
  31.   <HIREDATE>22-FEB-81</HIREDATE>
  32.   <SAL>1250</SAL>
  33.   <COMM>500</COMM>
  34.   <DEPTNO>30</DEPTNO>
  35.  </EMP>
  36.  <EMP>
  37.   <EM
  38. PNO>7566</EMPNO>
  39.   <ENAME>JONES</ENAME>
  40.   <JOB>MANAGER</JOB>
  41.   <MGR>7839</MGR>
  42.   <HIREDATE>02-APR-81</HIREDATE>
  43.   <SAL>2975</SAL>
  44.   <DEPTNO>20</DEPTNO>
  45.  </EMP>
  46.  <EMP>
  47.   <EMPNO>7654</EMPNO>
  48.   <ENAME>MA
  49. RTIN</ENAME>
  50.   <JOB>SALESMAN</JOB>
  51.   <MGR>7698</MGR>
  52.   <HIREDATE>28-SEP-81</HIREDATE>
  53.   <SAL>1250</SAL>
  54.   <COMM>1400</COMM>
  55.   <DEPTNO>30</DEPTNO>
  56.  </EMP>
  57.  <EMP>
  58.   <EMPNO>7698</EMPNO>
  59.   <ENAME>BLAKE</E
  60. NAME>
  61.   <JOB>MANAGER</JOB>
  62.   <MGR>7839</MGR>
  63.   <HIREDATE>01-MAY-81</HIREDATE>
  64.   <SAL>2850</SAL>
  65.   <DEPTNO>30</DEPTNO>
  66.  </EMP>
  67.  <EMP>
  68.   <EMPNO>7782</EMPNO>
  69.   <ENAME>CLARK</ENAME>
  70.   <JOB>MANAGER</JOB>
  71.  
  72. <MGR>7839</MGR>
  73.   <HIREDATE>09-JUN-81</HIREDATE>
  74.   <SAL>2450</SAL>
  75.   <DEPTNO>10</DEPTNO>
  76.  </EMP>
  77.  <EMP>
  78.   <EMPNO>7788</EMPNO>
  79.   <ENAME>SCOTT</ENAME>
  80.   <JOB>ANALYST</JOB>
  81.   <MGR>7566</MGR>
  82.   <HIREDATE
  83. >09-DEC-82</HIREDATE>
  84.   <SAL>3000</SAL>
  85.   <DEPTNO>20</DEPTNO>
  86.  </EMP>
  87.  <EMP>
  88.   <EMPNO>7839</EMPNO>
  89.   <ENAME>KING</ENAME>
  90.   <JOB>PRESIDENT</JOB>
  91.   <HIREDATE>17-NOV-81</HIREDATE>
  92.   <SAL>5000</SAL>
  93.   <DE
  94. PTNO>10</DEPTNO>
  95.  </EMP>
  96.  <EMP>
  97.   <EMPNO>7844</EMPNO>
  98.   <ENAME>TURNER</ENAME>
  99.   <JOB>SALESMAN</JOB>
  100.   <MGR>7698</MGR>
  101.   <HIREDATE>08-SEP-81</HIREDATE>
  102.   <SAL>1500</SAL>
  103.   <COMM>0</COMM>
  104.   <DEPTNO>30</
  105. DEPTNO>
  106.  </EMP>
  107.  <EMP>
  108.   <EMPNO>7876</EMPNO>
  109.   <ENAME>ADAMS</ENAME>
  110.   <JOB>CLERK</JOB>
  111.   <MGR>7788</MGR>
  112.   <HIREDATE>12-JAN-83</HIREDATE>
  113.   <SAL>1100</SAL>
  114.   <DEPTNO>20</DEPTNO>
  115.  </EMP>
  116.  <EMP>
  117.   <EMPN
  118. O>7900</EMPNO>
  119.   <ENAME>JAMES</ENAME>
  120.   <JOB>CLERK</JOB>
  121.   <MGR>7698</MGR>
  122.   <HIREDATE>03-DEC-81</HIREDATE>
  123.   <SAL>950</SAL>
  124.   <DEPTNO>30</DEPTNO>
  125.  </EMP>
  126.  <EMP>
  127.   <EMPNO>7902</EMPNO>
  128.   <ENAME>FORD</E
  129. NAME>
  130.   <JOB>ANALYST</JOB>
  131.   <MGR>7566</MGR>
  132.   <HIREDATE>03-DEC-81</HIREDATE>
  133.   <SAL>3000</SAL>
  134.   <DEPTNO>20</DEPTNO>
  135.  </EMP>
  136.  <EMP>
  137.   <EMPNO>7934</EMPNO>
  138.   <ENAME>MILLER</ENAME>
  139.   <JOB>CLERK</JOB>
  140.  
  141. <MGR>7782</MGR>
  142.   <HIREDATE>23-JAN-82</HIREDATE>
  143.   <SAL>1300</SAL>
  144.   <DEPTNO>10</DEPTNO>
  145.  </EMP>
  146. </EMP_DETAILS>
  147.  
  148.  
Dec 30 '09 #1
4 9689
I found it very useful .... coordination of oracle and xml is good.
Dec 31 '09 #2
Hi
I Have a doubt.
These are my emp table records
EmpId EmpName
1 AAA
2 BBB
3 CCC

I want to generate xml in below format.
<emp>
<1>AAA</1>
<2>BBB</2>
<3>CCC</3>
</emp>

Can you kindly help me?
Nov 17 '10 #3
Thank you for this exampel it has helped me alot. Much appreciated.
Jun 22 '11 #4
n3xus
1
Thanks for your post. It helped me
Apr 4 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text...
0
by: DaveS | last post by:
Hello, I'm using Oracle 9.2 and would like to automatically save a copy of each deleted record in XML format to another database table. There...
1
by: Da-Breegster | last post by:
Hi. I'm attempting to write a roguelike (think nethack, crawl, angband, tome, adom, and yes, I suppose even rogue) in Perl and I've ran into a...
1
by: Siva | last post by:
Hi, I have a 3 tier ASP.Net app for a handheld which needs to fetch orders from database via a DAL and populate it in a gridview using...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but...
30
by: Charles Law | last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this once before, but ...". Two users are both looking at the same data,...
8
by: rpsetzer | last post by:
I have to create a big web application and I was thinking of using a data layer. For each entity in the database, I'll define a class that maps the...
4
by: winsletmathew | last post by:
I need the backup of only the structure of database, not the data. I tried, this procedure , but it is copying the whole database create ...
6
by: rakesh19 | last post by:
Hi, I am building an application similar to google autosuggest. I want to fetch the list of items to be suggested using PHP. Those items are...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.