Writing tables to xml

Willem-Jan Overink 1

I was
searching information about writing tables to a XML file. The process consists
of 2 step

  • Creating the XML file
  • Writing the XML to disk

The last step is easy (see https://technology.amis.nl/blog/?p=2853 Of my colleague Marco).

This post
concentrates on the first step.

I decided
to create a XML file based on the standard DEPT and EMP tables. The following
code did the trick:

 PROCEDURE writexml
      CURSOR xmlcur IS                                                                                                     				  
         SELECT xmlelement("DEPARTMENTS", 
	       xmlelement("DEPARTMENT", xmlattributes(d.loc as "LOC"),
	             xmlforest(d.dname as "NAME", d.deptno as "DEPTNO")
		            ,xmlagg(xmlelement("EMPLOYEE", xmlforest(e.empno as "EMPNO", e.ename as "NAME", e.job as "JOB", e.hiredate as "HIREDATE", e.sal as "SAL"))
    FROM   emp e                                                                                                        
     ,      dept d                                                                                                       
	GROUP BY d.deptno, d.dname, d.loc ;
	l_emp xmltype;                                                                                     
   		open xmlcur;
		fetch xmlcur into l_emp;
                close xmlcur;
  		dbms_xslprocessor.clob2file( l_emp.getclobval( ) , 'TEMP', 'test.xml' );
  • The
    XMLelement is used to create an XML element based on the column value.
  • With the XMLAgg it is possible to generate collections of xml elements.
  • XMLForest is used to generate several xml elements successively. It is the same
    if you are using several XMLelements.
  • With XMLConcat you can combine XMLType values.

The xml output
is the following:


One thought on “Writing tables to xml

  1. I wish I read this yesterday. It would save me some time today 🙂 I’ve archived the same with more code and UTL_FILE.

Comments are closed.

Next Post

Fear for renaming (refactoring) ADF BC objects in JDeveloper is not unfounded

Growing insight during development of a project often results in a wish to rename a BC object (entity object, view object, association, view object attribute, alias, usage, etc.) in JDeveloper. Or in JHeadstart to rename objects like a group, item or a region. Often, developers are not renaming objects because it is […]
%d bloggers like this: