Oracle如何操作XML

來源:互聯網
上載者:User

最近在研究Oracle PLSQL中對於XML的系列操作。結合工作中使用的知識和參考資料整理出以下相關內容:

一 如何產生XML檔案:

1、使用dbms_xmlquery和utl_file內建包(scott使用者執行)

CREATE OR REPLACE DIRECTORY xml_dir AS 'd:\app\xml';        DROP SEQUENCE seq_filename;  CREATE SEQUENCE seq_filename       MINVALUE 10000      MAXVALUE 99999       INCREMENT BY 1       START WITH 10000       NOCYCLE;
DECLARE    v_filename  Varchar2(50)  := 'Empmsg'||to_char(seq_filename.nextval)||'.xml';      xml_str     clob;      xml_file    utl_file.file_type;      offset      number;      buffer      varchar2(32767);      buffer_size number;  BEGIN    offset      := 1;      buffer_size := 3000;      xml_file := utl_file.fopen('XML_DIR', v_filename, 'w');      xml_str  := dbms_xmlquery.getxml('select empno,                                               ename,                                               job,                                               mgr,                                               hiredate,                                               sal,                                               comm,                                               deptno                                        from emp');              while (offset < dbms_lob.getlength(xml_str)) loop        buffer := dbms_lob.substr(xml_str, buffer_size, offset);        utl_file.put(xml_file, buffer);        utl_file.fflush(xml_file);        offset := offset + buffer_size;      end loop;                utl_file.fclose(xml_file);  END;

2、使用XMLELEMENT系列內建函數返回xml(sys使用者執行)

DECLARE    v_filename  Varchar2(50)  := 'Empmsg'||to_char(scott.seq_filename.nextval)||'.xml';      xml_str     clob;      xml_file    utl_file.file_type;      offset      number;      buffer      varchar2(32767);      buffer_size number;  BEGIN    offset      := 1;      buffer_size := 3000;      xml_file := utl_file.fopen('XML_DIR', v_filename, 'w');      SELECT XMLElement("DEPARTMENT"                 , XMLAttributes( department_id as "ID"                                , department_name as "NAME"                                )                   , XMLElement("EMPLOYEES"                             , (SELECT XMLAgg( XMLElement("EMPLOYEE"                                                         , XMLForest(employee_id as "ID"                                                                    ,first_name||' '||last_name as "NAME"                                                                    )                                                           )                                               )                                   FROM hr.employees emp                                  WHERE emp.department_id = dept.department_id                                  )                                )                  ).getclobval() INTO xml_str       FROM hr.departments dept       WHERE department_id = 20;              while (offset < dbms_lob.getlength(xml_str)) loop        buffer := dbms_lob.substr(xml_str, buffer_size, offset);        utl_file.put(xml_file, buffer);        utl_file.fflush(xml_file);        offset := offset + buffer_size;      end loop;                utl_file.fclose(xml_file);  END;

本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/database/Oracle/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.