473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to fetch data from oracle in xml format?

These are my emp table records
EmpId EmpName
1 AAA
2 CCC
3 BBB

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

What is the required sql query?
Nov 17 '10 #1
4 3273
amitpatel66
2,367 Expert 2GB
Are you looking at something like this:

Expand|Select|Wrap|Line Numbers
  1. with t as (select 1 empid, 'AAA' name from dual
  2. union all select 2, 'BBB' from dual
  3. union all select 3,'CCC' from dual)
  4. SELECT XMLELEMENT("EMP",XMLAGG(XMLELEMENT(empid,XMLATTRIBUTES(empid),name))) from t
  5. /
  6.  
  7. <EMP><EMPID EMPID="1">AAA</EMPID><EMPID EMPID="2">BBB</EMPID><EMPID EMPID="3">CCC</EMPID></EMP>
  8.  
  9.  
Nov 17 '10 #2
amitpatel66
2,367 Expert 2GB
The way you want the XML to be generated will fail in parsing because XML tag changes as per empid.

You can:

Expand|Select|Wrap|Line Numbers
  1. with t as (select 1 empid,'AAA' name FROM dual
  2. union all select 2, 'BBB' from dual
  3. union all select 3,'CCC' from dual)
  4. select col from
  5. (SELECT '<EMP>' col,0 FROM dual
  6. UNION
  7. SELECT '<'||empid||'>'||name||'</'||empid||'>' col,rownum rn from t
  8. UNION
  9. SELECT '</EMP>',NULL FROM dual
  10. ORDER BY 2 NULLS LAST)
  11. /
  12.  
  13. <EMP>
  14. <1>AAA</1>
  15. <2>BBB</2>
  16. <3>CCC</3>
  17. </EMP>
  18.  
  19.  
Nov 17 '10 #3
Is the sql query not possible by using xml functions like xmlelement,xmlquery?
Nov 18 '10 #4
amitpatel66
2,367 Expert 2GB
You can try something like this:

Expand|Select|Wrap|Line Numbers
  1. with t as (select 1 empid,'AAA' ename from dual
  2. union all select 2,'BBB' from dual)
  3. SELECT  REPLACE(col1,'ROWSET','EMP') FROM
  4. (SELECT SYS_XMLAGG (XMLELEMENT(ename,XMLATTRIBUTES(empid),ename)) col1 from t)
  5. /
  6.  
  7. col1
  8. -----
  9.  
  10. "<?xml version="1.0"?>
  11. <EMP>
  12. <ENAME EMPID="1">AAA</ENAME><ENAME EMPID="2">BBB</ENAME></EMP>
  13. "
  14.  
  15.  
Nov 18 '10 #5

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

Similar topics

0
by: odmoney | last post by:
Hello, Can anyone point me in the right direction on how one would fetch an NCLOB datatype into c++ ? I have read a lot of documentation on the OCI API as well as Oracle LOBs, but cannot get...
1
by: Srinivasan R | last post by:
Hi, I am trying to get asynchronous fetching of data from sql server database. I would like to show the progress of fetching records in GUI. Is there any simple/complex steps for the same in C#...
5
by: carl.barrett | last post by:
Hi, I am writing to a text file the records in an access table that is to be loaded into an Oracle system. However, I have been told that the date fields in the records I have exported to the...
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 still i am getting error. --======Start...
9
by: Jenden0 | last post by:
I'm new to C# (and Microsoft in general) so this may be a simple problem, but I haven't been able to figure it out yet. I've got a database with a number of different tables and I want the user...
2
by: ameen.abdullah | last post by:
Hi Guys, I have a datagrid which is binded to dataset which is gettting data from xml. Now i need to know if i edit or add data to the datagrid, whats the best possible way to fetch it? .. i...
0
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE TO...
1
by: Frank Moyles | last post by:
I have two simple function in PHP that I want to convert to C# - function Doit($enc_user_name, $enc_password) { $result = $mysqli->query("call sp_userAuth('$enc_user_name','$enc_password')"); ...
0
by: =?Utf-8?B?S3VydA==?= | last post by:
I was wondering if anyone knew if there was a simple way to convert a Data Interchange Format (.dif) file (see:http://en.wikipedia.org/wiki/Data_Interchange_Format) into a dataset using the native...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.