2010-02-08 98 views
2

我的程序甲骨文的Xml生成節點空

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 [email protected] 
2 - - - 23 - - - - - - - - - - [email protected] 

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>[email protected]</E_MAIL> 
</ROW> 
<ROW> 
<ID>1</ID> 
<SAL>23</SAL> 
<E_MAIL>[email protected]</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>[email protected]</E_MAIL> 
<ROW> 
<ID>2</ID> 
<NAME></NAME> 
<SAL>23</SAL> 
<HIREDATE></HIREDATE> 
<DEPTNO></DEPTNO> 
<E_MAIL>[email protected]</E_MAIL> 
</ROW> 
</ROWSET> 

回答

2

我們可以使用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> 
+0

APC謝謝你...工作... – Chicharito 2010-02-08 11:29:10