Oracle Xml生成节点null

时间:2010-02-08 08:17:08

标签: xml oracle

我的程序

SELECT dbms_xmlgen.getxml('select * from '||i_tablename) into xml_data from dual;

doc := DBMS_XMLDOM.NewDOMDocument(xml_data);

DBMS_XMLDOM.WRITETOFILE(doc, 'XML_DIR/'||v_tablename);

我的表

ID Name SAL HIREDATE DEPTNO E_MAIL
1 xyz 23 01-JAN-00 2 bla@bla.com
2 - - - 23 - - - - - - - - - - asd@blabla.com

xml输出

<?xml version="1.0" ?>
<ROWSET>
<ROW>
<ID>1</ID>
<NAME>xyz</NAME>
<SAL>23</SAL>
<HIREDATE>01-JAN-00</HIREDATE>
<DEPTNO>2</DEPTNO>
<E_MAIL>bla@bla.com</E_MAIL>
</ROW>
<ROW>
<ID>1</ID>
<SAL>23</SAL>
<E_MAIL>bla@bla.com</E_MAIL>
</ROW>
</ROWSET>

我想这样做

<?xml version="1.0" ?>
<ROWSET>
<ROW>
<ID>1</ID>
<NAME>xyz</NAME>
<SAL>23</SAL>
<HIREDATE>01-JAN-00</HIREDATE>
<DEPTNO>2</DEPTNO>
<E_MAIL>bla@bla.com</E_MAIL>
<ROW>
<ID>2</ID>
<NAME></NAME>
<SAL>23</SAL>
<HIREDATE></HIREDATE>
<DEPTNO></DEPTNO>
<E_MAIL>asd@blabla.com</E_MAIL>
</ROW>
</ROWSET>

1 个答案:

答案 0 :(得分:3)

我们可以使用DBMS_XMLGEN.SETNULLHANDLING()来控制XQuery处理NULL的方式。将其设置为2会生成一个空标记。不幸的是,操纵结果集的格式需要我们指定一个上下文,因此会产生稍微冗长的语法。

SQL> set long 5000
SQL> var c clob
SQL>
SQL> declare
  2      ctx dbms_xmlgen.ctxhandle;
  3  begin
  4      ctx := dbms_xmlgen.newcontext('select * from emp where deptno = 10');
  5      -- set EMPTY_TAG  flag
  6      dbms_xmlgen.SETNULLHANDLING (ctx, 2);
  7      :c := dbms_xmlgen.getxml(ctx);
  8  end;
  9  /

PL/SQL procedure successfully completed.

SQL>
SQL> print c

C
--------------------------------------------------------------------------------
<?xml version="1.0"?>
<ROWSET>
 <ROW>
  <EMPNO>7782</EMPNO>
  <ENAME>BOEHMER</ENAME>
  <JOB>MANAGER</JOB>
  <MGR>7839</MGR>
  <HIREDATE>09-JUN-81</HIREDATE>
  <SAL>2450</SAL>
  <COMM/>
  <DEPTNO>10</DEPTNO>
  <LEAVE_DATE/>
 </ROW>
 <ROW>
  <EMPNO>7839</EMPNO>
  <ENAME>SCHNEIDER</ENAME>
  <JOB>PRESIDENT</JOB>
  <MGR/>
  <HIREDATE>17-NOV-81</HIREDATE>
  <SAL>5000</SAL>
  <COMM/>
  <DEPTNO>10</DEPTNO>
  <LEAVE_DATE/>
 </ROW>
 <ROW>
  <EMPNO>8070</EMPNO>
  <ENAME>DUGGAN</ENAME>
  <JOB>SALES</JOB>
  <MGR>7782</MGR>
  <HIREDATE>19-NOV-09</HIREDATE>
  <SAL>2500</SAL>
  <COMM/>
  <DEPTNO>10</DEPTNO>
  <LEAVE_DATE/>
 </ROW>
</ROWSET>


SQL>