0

我有一个 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>

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

  • 报告标题
  • 数字
  • 地位
  • 电子邮件
  • 登录ID
  • 地点
  • 提交日期
  • 开始日期
  • Finsih_date
  • 更新日期
  • 姓名
  • 年度使用
  • 目的

我试过了,我得到了关注:

<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>
4

1 回答 1

0

我认为这样的事情会有所帮助,试试 google for xmldom built in package in 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;
于 2013-04-05T11:33:16.600 回答