从数据库表创建XML

时间:2013-04-04 18:58:19

标签: plsql oracle10g xml-serialization xsd plsqldeveloper

我有一个XML文件示例。现在我想用我拥有的SQL表数据生成原始XML。

示例XML如下所示:

<?xml version="1.0"?>
<EmployeeSet  xmlns=" http://www.example.com/Employee.xsd " xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<DefaultDeptt xsi:nil="true"/> 
<Report>
    <Report  Title="Yearly" Number="S678"> 
        <Status>Current</Status> 
        <Deptt xsi:nil="true"/> 
        <Employee LastName="Name" FirstName="Simple" EMail="simple@organization.org" Login="simple"/> 
        <Location>Builiding 1</Location> 
        <SubmissionDate xsi:nil="true"/> 
        <ReportStartDate>2011-05-05</ReportStartDate> 
        <ReportFinishDate xsi:nil="true"/> 
        <ReportExpirationDate>2014-05-05</ReportExpirationDate> 
        <RenewalDate xsi:nil="true"/> 
        <Records> 
            <RecordsInfo Name="Paper"> 
            <UsageRecords AnnualUse="5o0" Purpose="printing"/> 
            </RecordInfo> 
        </Record> 
    <Staff/> 
</Report>

我从中获取数据的表具有以下结构:

  • REPORT_TITLE
  • 状态
  • 姓氏
  • 如first_name
  • 电子邮件
  • LOGIN_ID
  • 位置
  • submission_date
  • 起始日期
  • Finsih_date
  • RENEWAL_DATE
  • 名称
  • annual_use
  • 目的

我尝试了手,我正在追随:

<EmployeeSet xmlns="http://www.example.com/Employee.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <DefaultDeptt xsi:nil="true"></DefaultDeptt>        
    <report_title>TESTINGNEW</report_TITLE>      
    <NUMBER>AC10006</NUMBER>    
    <STATUS>Approved</STATUS>       
    <LAST_NAME>XYZ</LAST_NAME>       
    <FIRST_NAME>ABC</PI_FIRST_NAME>       
    <EMAIL>ABC.XYZ@gmail.com</EMAIL>       
    <LOGIN_ID>ABCXYZ</LOGIN_ID>       
    <LOCATION> </ LOCATION>       
                                                                                         <SUBMISSION_DATE>2013-03-25</SUBMISSION_DATE>                                                  <START_DATE>2013-03-25</START_DATE>           
    <FINISH_DATE>2013-03-25</FINISH_DATE>        
    <RENEWAL_DATE>2014-01-9</RENEWAL_DATE>        
    <NAME>PAPER</NAME>        
    <ANNUAL_USE>670</ANNUAL_USE>       
    <PURPOSE> PRINTING</PURPOSE>        
</EmployeeSet>

1 个答案:

答案 0 :(得分:0)

我认为像这样的东西会对谷歌中的xmldom在oracle中构建的包中有所帮助

declare
           xml_content CLOB:=null;  
           doc xmldom.domdocument;  
           main_node xmldom.DOMNode;                                                                           
           root_node xmldom.DOMNode;                                                                           
           root_elmt xmldom.DOMElement;                                                                         
           transmissionHeaderNode xmldom.DOMNode;                                                              
           transmissionHeaderElement xmldom.DOMElement;                                                               
           item_node xmldom.DOMNode;
           item_elmt xmldom.DOMElement;
           item_text xmldom.DOMText;
           redx_elmt xmldom.DOMElement;
begin
            --some cursor
            for c1 in (
                        select * from (yourtable)
                       )
            loop
-- here you loop all you're fetches from table
              doc := xmldom.newdomdocument;                                                                       
              main_node := xmldom.makenode(doc);                                                                  
              xmldom.setversion(doc,'1.0');            
              dbms_xmldom.setcharset(doc, 'UTF-8'); -- UTF-8
              root_elmt := xmldom.createelement(doc, 'Some header')
              root_node := xmldom.appendchild( main_node, xmldom.makenode(root_elmt));               
              transmissionheaderelement := xmldom.createelement(doc, 'XML_DOCUMENT');
              transmissionheadernode := xmldom.appendchild(root_node, xmldom.makenode(transmissionheaderelement));
        -- lets say in cursor you have location field
              item_elmt := xmldom.createelement(doc, 'Location'); 
              item_node := xmldom.appendchild(transmissionheadernode, xmldom.makenode(item_elmt));                
              item_text := xmldom.createTextNode(doc, c1.Location);                             
              item_node := xmldom.appendchild(item_node, xmldom.makenode(item_text));
            .
            .
            .
            -- before end of loop
               dbms_lob.createtemporary(xml_content, true, dbms_lob.call);
               xmldom.writetoclob(doc, xml_content);               
               xmldom.freedocument(doc);             


   end loop;