需要解析的xml文件:people.xml
1 <?xml version="1.0"?>
2 <PEOPLE>
3 <PERSON PERSONID="E01">
4 <NAME>Tony Blair</NAME>
5 <ADDRESS>10 Downing Street, London, UK</ADDRESS>
6 <TEL>(061) 98765</TEL>
7 <FAX>(061) 98768</FAX>
8 <EMAIL>
[email protected]</EMAIL>
9 </PERSON>
10 <PERSON PERSONID="E02">
11 <NAME>Bill Clinton</NAME>
12 <ADDRESS>White House, USA</ADDRESS>
13 <TEL>(001) 6400 98765</TEL>
14 <FAX>(001) 6400 98769</FAX>
15 <EMAIL>
[email protected]</EMAIL>
16 </PERSON>
17 <PERSON PERSONID="E03">
18 <NAME>Tom Cruise</NAME>
19 <ADDRESS>57 Jumbo Street, New York, USA</ADDRESS>
20 <TEL>(001) 4500 67859</TEL>
21 <FAX>(001) 4500 67895</FAX>
22 <EMAIL>
[email protected]</EMAIL>
23 </PERSON>
24 <PERSON PERSONID="E04">
25 <NAME>Linda Goodman</NAME>
26 <ADDRESS>78 Crax Lane, London, UK</ADDRESS>
27 <TEL>(061) 54 56789</TEL>
28 <FAX>(061) 54 56772</FAX>
29 <EMAIL>
[email protected]</EMAIL>
30 </PERSON>
31 </PEOPLE>
我以scott用户为例,新建表:
1 CREATE TABLE PEOPLE
2 (
3 PERSONID VARCHAR2(4) PRIMARY KEY,
4 NAME VARCHAR2(50),
5 ADDRESS VARCHAR2(200),
6 TEL VARCHAR2(20),
7 FAX VARCHAR2(20),
8 EMAIL VARCHAR2(100)
9 );
解析并持久化的存储过程代码:
1 /**
2 *file_path 需要解析的XML文件路径 如:D:\OracleTest\people.xml
3 *log_path 保存日志的文件的路径 如: D:\OracleTest\xmllog.txt
4 **/
5
6 CREATE OR REPLACE PROCEDURE addPerson(file_path VARCHAR2,log_path VARCHAR2)
7 AS
8 --//XML解析器
9 xmlPar XMLPARSER.parser := XMLPARSER.NEWPARSER;
10 --//DOM文档对象
11 doc xmldom.DOMDocument;
12 len integer;
13 personNodes xmldom.DOMNodeList;
14 chilNodes xmldom.DOMNodeList;
15 tempNode xmldom.DOMNode;
16 tempArrMap xmldom.DOMNamedNodeMap;
17 --================================
18 --以下变量用于获取XML节点的值
19 pid varchar2(4);
20 name varchar2(50);
21 address varchar2(200);
22 tel varchar2(20);
23 fax varchar2(20);
24 email varchar(100);
25 tmp integer;
26 --================================
27 BEGIN
28 xmlPar := xmlparser.newParser;
29 xmlparser.setErrorLog( xmlPar, log_path);
30 xmlparser.parse(xmlPar, file_path);
31 doc := xmlparser.getDocument( xmlPar );
32 -- 释放解析器实例
33 xmlparser.freeParser(xmlPar);
34 -- 获取所有PERSON元素
35 personNodes := xmldom.getElementsByTagName( doc, 'PERSON' );
36 len := xmldom.getLength( personNodes );
37 --遍历所有PERSON元素
38 FOR i in 0..len-1
39 LOOP
40 --获取第i个PERSON
41 tempNode := xmldom.item( personNodes, i );
42 --所有属性
43 tempArrMap := xmldom.getAttributes(tempNode);
44 --获取PERSONID的值
45 pid := xmldom.getNodeValue(xmldom.getNamedItem(tempArrMap,'PERSONID'));
46 --获取子元素的值
47 chilNodes := xmldom.getChildNodes(tempNode);
48 tmp := xmldom.GETLENGTH( chilNodes );
49 name := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 0 )));
50 address := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 1 )));
51 tel := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 2 )));
52 fax := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 3 )));
53 email := xmldom.getNodeValue(xmldom.getFirstChild(xmldom.item( chilNodes, 4 )));
54 --插入数据
55 INSERT INTO PEOPLE VALUES (pid,name,address,tel,fax,email);
56 COMMIT;
57 END LOOP;
58 -- 释放文档对象
59 xmldom.freeDocument(doc);
60 EXCEPTION
61 WHEN OTHERS THEN
62 DBMS_output.PUT_LINE(SQLERRM);
63 END addPerson;
有许多朋友都说执行时出现错误,现在我把我在Oracle9i使用的截图贴出来,以作参考:
存储过程截图