473,771 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data Fetch in XML Structure using DBMS_XMLGEN

amitpatel66
2,367 Recognized Expert Top Contributor
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.NEW CONTEXT

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.SET ROWSETTAG

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.SET ROWTAG

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.GET XML

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 9957
ananthaisin
15 New Member
I found it very useful .... coordination of oracle and xml is good.
Dec 31 '09 #2
premkumar sp
4 New Member
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
Grant Ryan
1 New Member
Thank you for this exampel it has helped me alot. Much appreciated.
Jun 22 '11 #4
n3xus
1 New Member
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
14673
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 file looks like this: --------
0
3020
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 would be one XML record for each deleted record. I'd like to store the XML record in a CLOB. I've created a table and added a trigger to capture the delete event but I'm not sure how to create the XML document. Each deleted record is comprised of a single row from a parent table and multiple...
1
5111
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 problem regarding the data structure for the map and handling it easily. A map is one 2D level, a simple grid. Each tile on the grid will be a hash of data about the grid. The map object itself has other bits of data, so that's why I don't just make the object an anonymous array: $map->{Map} = {...
1
2313
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 objectdatasource. In the search page I have a few parameters and when the search button is clicked I am sending those parameters via query string ( I need to do this since my app will run in a handheld which has certain restrictions). In the order list page, I am passing those parameters to the DAL as...
1
13669
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 still i am getting error. --======Start procedure============= Create PROCEDURE get_timedout_scripts (
30
3405
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, from a database. One user changes the data and commits it. How does the other user get the updated view without polling for changes? Is there some sort of callback mechanism that can be set up on the dataset or connection? TIA
8
1941
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 table structure, having sub-objects for each foreign key, having insert/delete/update methods, the usual deal. Yet, I am very concerned about performance. For example, there are lots of cases when I may just be needing the employee name. Yet using this model, I will have to instantiate an...
4
4430
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 Procedure BackupAlldatabases as
6
2100
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 stored in a database. Is it a good idea to make a database connection everytime or store them in a batch in some kind of PHP data structure. Latter seems more viable. What kind of data structure in PHP is best suited. Is it arrays or some kind of storables. I am new to PHP. Also, the no. of items will...
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.