从数据库表创建XML

问题描述 投票:0回答:1

我有一个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="[email protected]" 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
  • 状态
  • 名字
  • 电子邮件
  • 登录ID
  • 地点
  • 提交日期
  • 开始日期
  • Finsih_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>[email protected]</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>
plsql oracle10g xml-serialization xsd plsqldeveloper
1个回答
0
投票

我觉得像这样的东西会有用,试试谷歌在oracle中内置的xmldom包

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;
© www.soinside.com 2019 - 2024. All rights reserved.