2013-04-04 119 views
0

我有一個XML文件的示例。現在我想用我所擁有的sql表數據生成一個原始的xml。從數據庫表創建xml

示例XML看起來是這樣的:

<?xml version="1.0"?> 
<EmployeeSet xmlns=" http://www.abc.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
  • 狀態
  • 姓氏
  • first_nam Ë
  • 電子郵件
  • LOGIN_ID
  • 位置
  • submission_date
  • 起始日期
  • Finsih_date
  • RENEWAL_DATE
  • annual_use
  • 目的

請幫我生成這個XML。這很緊急。

我想我的手和我得到以下幾點:

`

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

`

+1

[你有什麼***試過?](http://www.whathaveyoutried.com) – 2013-04-04 19:04:32

+0

我試圖使用以下查詢但結果不相同:
'select XMLElement(「EmployeeSet」,XMLAttributes('http://www.abc.com.Employee.xsd'as「xmlns」,'http:// www.w3.org/2001/XMLSchema'as「xmlns:xsd」,「http://www.w3.org/2001/XMLSchema-instance」爲「xmlns:xsi」), XMLElement(「DefaultDeptt」,XMLAttributes ('true'as「xsi:nil」)), XMLForest(title,number,status,last_name,first_name,email,login_id,location,submission_date, startdate,finishdate,renewaldate,name,annual_use,purpose) XML「from xyz.emp_temp;' – user2171679 2013-04-04 19:12:54

+0

我是使用oracle的XML新手。從我所學習的教程都是這些命令。 – user2171679 2013-04-04 19:13:41

回答

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; 
+0

嗨VedranI嘗試使用上述指定的代碼。但我的pl-sql開發人員不支持此代碼 – user2171679 2013-04-05 18:49:43

+0

請給我一個支持dbms_xmldom的軟件列表。我準備了一個程序來生成我的xml – user2171679 2013-04-08 20:06:24

+0

嘗試此鏈接以排除故障xmldom也許您沒有在您的O11g中安裝DBMS_XMLDOM https://forums.oracle.com/forums/thread.jspa?threadID=353424 – 2013-04-10 08:23:07