Oracle 匯出匯入XML,準確來說是伺服器端的匯入匯出,用戶端的話需要事先通過應用程式傳送到伺服器端目錄下..一般應用的話是不會在伺服器端進行解析的,除非是DBA操作,作為學習看瞭解下,以下通過測試,測試環境ORACLE 9I 版本號碼,9.0.2.1
建立測試表:
CREATE TABLE PEOPLE
(
PERSONID VARCHAR2(10) PRIMARY KEY,
NAME VARCHAR2(20),
ADDRESS VARCHAR2(60),
TEL VARCHAR2(20),
FAX VARCHAR2(20),
EMAIL VARCHAR2(40)
);
XML檔案資料,儲存為people.xml,放置於D盤下的TEST目錄下:
<?xml version="1.0"?>
<PEOPLE>
<PERSON PERSONID="E01">
<NAME>Tony Blair</NAME>
<ADDRESS>10 Downing Street, London, UK</ADDRESS>
<TEL>(061) 98765</TEL><FAX>(061) 98768</FAX>
<EMAIL>blair@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E02">
<NAME>Bill Clinton</NAME>
<ADDRESS>White House, USA</ADDRESS>
<TEL>(001) 6400 98765</TEL><FAX>(001) 6400 98769</FAX>
<EMAIL>bill@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E03">
<NAME>Tom Cruise</NAME>
<ADDRESS>57 Jumbo Street, New York, USA</ADDRESS>
<TEL>(001) 4500 67859</TEL><FAX>(001) 4500 67895</FAX>
<EMAIL>cruise@everywhere.com</EMAIL>
</PERSON>
<PERSON PERSONID="E04">
<NAME>Linda Goodman</NAME>
<ADDRESS>78 Crax Lane, London, UK</ADDRESS>
<TEL>(061) 54 56789</TEL><FAX>(061) 54 56772</FAX>
<EMAIL>linda@everywhere.com</EMAIL>
</PERSON>
</PEOPLE>
匯入XML檔案到資料表中的預存程序:
/** *file_path 需要解析的XML檔案路徑 如:D:/Test/people.xml *log_path 儲存日誌的檔案的路徑 如: D:/Test/xmllog.txt **/ CREATE OR REPLACE PROCEDURE addPerson(file_path VARCHAR2,log_path VARCHAR2) AS --//XML解析器 xmlPar XMLPARSER.parser := XMLPARSER.NEWPARSER; --//DOM文檔對象 doc xmldom.DOMDocument; len integer; personNodes xmldom.DOMNodeList; chilNodes xmldom.DOMNodeList; tempNode xmldom.DOMNode; tempArrMap xmldom.DOMNamedNodeMap; --================================ --以下變數用於擷取XML節點的值 pid varchar2(4); name varchar2(50); address varchar2(200); tel varchar2(20); fax varchar2(20); email varchar(100); tmp integer; --================================ BEGIN xmlPar := xmlparser.newParser; xmlparser.setErrorLog( xmlPar, log_path); xmlparser.parse(xmlPar, file_path); doc := xmlparser.getDocument( xmlPar ); -- 釋放解析器執行個體 xmlparser.freeParser(xmlPar); -- 擷取所有PERSON元素 personNodes := xmldom.getElementsByTagName( doc, 'PERSON' ); len := xmldom.getLength( personNodes ); --遍曆所有PERSON元素 FOR i in 0..len-1 LOOP --擷取第i個PERSON tempNode := xmldom.item( personNodes, i ); --所有屬性 tempArrMap := xmldom.getAttributes(tempNode); --擷取PERSONID的值 pid := xmldom.getNodeValue(xmldom.getNamedItem(tempArrMap,'PERSONID')); --擷取所有子節點 chilNodes := xmldom.getChildNodes(tempNode); --擷取子節點的個數 tmp := xmldom.GETLENGTH( chilNodes ); --擷取各個子節點的值 name := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 0 ))); address := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 1 ))); tel := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 2 ))); fax := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 3 ))); email := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 4 ))); --插入資料 INSERT INTO PEOPLE VALUES (pid,name,address,tel,fax,email); COMMIT; END LOOP; -- 釋放文檔對象 xmldom.freeDocument(doc); EXCEPTION WHEN OTHERS THEN DBMS_output.PUT_LINE(SQLERRM); END addPerson;
執行預存程序:
SQL> call addPerson('D:/test/people.xml','D:/test/people.log');
Method called
SQL> select * from people;
PERSONID NAME ADDRESS TEL FAX EMAIL
E01 Tony Blair 10 Downing Street, London, UK (061) 98765 (061) 98768 blair@everywhere.com
E02 Bill Clinton White House, USA (001) 6400 98765 (001) 6400 98769 bill@everywhere.com
E03 Tom Cruise 57 Jumbo Street, New York, USA (001) 4500 67859 (001) 4500 67895 cruise@everywhere.com
E04 Linda Goodman 78 Crax Lane, London, UK (061) 54 56789 (061) 54 56772 linda@everywhere.com
匯出的預存程序:輸入匯出路徑就可以了,下面是用來嵌套預存程序,由於沒有用到XML操作方面的,沒有去深究,但是還有一篇文章對匯出做了更詳細的說明,參考http://weiwu83.javaeye.com/blog/151855
--將資料庫中的資料匯出成xml檔案的過程
create or replace procedure db2xml(xmlfile varchar2) as
doc xmldom.DOMDocument;
ret xmldom.DOMNode;
peoplenode xmldom.DOMNode;
--遍曆整個people表的遊標
Cursor cur_people is select * from people;
--將people表中一行記錄轉換為元素
--並插入到DOM文檔對像根結點PEOPLE下的過程
procedure addperson(doc xmldom.DOMDocument,people xmldom.DOMNode,
v_pid varchar2,v_name varchar2,v_addr varchar2,
v_tel varchar2,v_fax varchar2,v_email varchar2)
is
personelem xmldom.DOMElement;
personnode xmldom.DOMNode;
itemelem xmldom.DOMElement;
itemnode xmldom.DOMNode;
text xmldom.DOMText;
begin
--建立PERSON結點
personelem := xmldom.cre