2017-02-22 61 views
0

我想從oracle數據庫創建一個xml文件。dbms_xmldom - 從oracle數據庫創建一個xml

我無法得到正確的格式,並想知道我是否可以得到一些幫助。

這是腳本的一部分:

l_record_element := dbms_xmldom.createElement(l_domdoc, 'record_type'); 
dbms_xmldom.setAttribute(l_record_element,'desc', r_dept.public1); 
l_record_node := dbms_xmldom.appendChild(l_dept_node,dbms_xmldom.makeNode(l_record_element)); 

我的輸出:

<record_type desc="Public"/> 

輸出我需要:

<record_type desc="Public">PUBLIC</record_type> 

謝謝!

回答

1

您需要創建並附加一個文本節點,其值爲PUBIC

演示示出了從提供記錄類型相同的光標來:

set serveroutput on 

declare 
    l_domdoc dbms_xmldom.domdocument; 
    l_dept_node dbms_xmldom.domnode; 
    l_record_node dbms_xmldom.domnode; 
    l_record_element dbms_xmldom.domelement; 
    l_record_text dbms_xmldom.domtext; 
    l_tmp_node dbms_xmldom.domnode; 
    l_xmltype xmltype; 
    l_buffer varchar2(32767); 
begin 
    l_domdoc := dbms_xmldom.newDOMDocument; --(xmltype('<data />')); 

    for r_dept in (select 'Public' as public1, 'PUBLIC' as public2 from dual) loop 
    l_dept_node := dbms_xmldom.makeNode(l_domdoc); 

    -- code you showed 
    l_record_element := dbms_xmldom.createElement(l_domdoc, 'record_type'); 
    dbms_xmldom.setAttribute(l_record_element,'desc', r_dept.public1); 
    l_record_node := dbms_xmldom.appendChild(l_dept_node, dbms_xmldom.makeNode(l_record_element)); 
    -- add a text node 
    l_record_text := dbms_xmldom.createTextNode(l_domdoc, r_dept.public2); 
    l_tmp_node := dbms_xmldom.appendChild(l_record_node, dbms_xmldom.makeNode(l_record_text)); 

    -- display the node for demo 
    l_xmltype := dbms_xmldom.getXmlType(l_domdoc); 
    dbms_xmldom.freeDocument(l_domdoc); 
    dbms_output.put_line(l_xmltype.getClobVal); 
    end loop; 
end; 
/

<record_type desc="Public">PUBLIC</record_type> 

PL/SQL procedure successfully completed.