How to generate XML using SQL query in Oracle

How to generate XML using SQL query in Oracle


1. Selecting single column from a table


SELECT  SYS_XMLAGG(SYS_XMLGEN(franchisee_codes))

   FROM franchisee_mapping

  WHERE username = '<username>';


The output will  be 


<?xml version="1.0"?>

<ROWSET>

<FRANCHISEE_CODES>WMHPUNBHAGWATIELEC_D</FRANCHISEE_CODES>

<FRANCHISEE_CODES>WMHPUNBHAGWATIELEC</FRANCHISEE_CODES>

</ROWSET>




2. Selecting multiple columns from a table


select 

DBMS_XMLGEN.getXML('select franchisee_codes,role 

FROM franchisee_mapping 

WHERE username = ''<username>'' and rownum<3' ) from dual;


The output will  be 


<?xml version="1.0"?>

<ROWSET>

 <ROW>

  <FRANCHISEE_CODES>WMHPUNBHAGWATIELEC_D</FRANCHISEE_CODES>

  <ROLE>WORK</ROLE>

 </ROW>

 <ROW>

  <FRANCHISEE_CODES>WMHPUNBHAGWATIELEC</FRANCHISEE_CODES>

  <ROLE>WORK</ROLE>

 </ROW>

</ROWSET>




Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post