select *
from all_directories CREATE DIRECTORY XML_DIR AS '/app/oracle/database/dir';
--创建测试表
create table test_xml(id number(2),content xmltype);
----将数据从文件读入数据库
INSERT INTO test_xml VALUES (1,XMLType(bfilename('XML_DIR','1.xml'),1));
--以clob形式读取xmltype字段
select id,x.content.getCLOBVal() from test_xml x;
--以string形式读取xmltype字段
select id,x.content.getstringVal() from test_xml x;
--判断节点存在性.
select * from test_xml t where existsNode(t.content,'/book') = 1;
--读取节点值
select id,extractvalue(content,'/book/title') from test_xml;
/**读取节点值同上(对于非xmltype型的字段,可先使用xmltype转换类型后,
**采用该种方式取值,即xmltype(表别名.字段名).extract(xmlpath))
**/
select id,x.content.extract('/book/title') from test_xml x;
--将查询结果转为xml格式.
SELECT DBMS_XMLGEN.getxml('select * from test_xml') xml_text FROM DUAL;
create or replace procedure parse_xml_file is
p_max_size number := dbms_lob.lobmaxsize;src_offset number := 1;
dst_offset number := 1;
lang_ctx number := nls_charset_id('UTF8');
default_csid constant integer := nls_charset_id('ZHS16GBK');
warning number;
l_file_number pls_integer := 0;
l_count number;
l_bfile bfile;
l_clob clob;
l_commitelement xmldom.DOMElement;
l_parser dbms_xmlparser.Parser;
l_doc dbms_xmldom.DOMDocument;
l_nl dbms_xmldom.DOMNodeList;
l_n dbms_xmldom.DOMNode;
rootnode dbms_xmldom.DOMNode;
parent_rootnode dbms_xmldom.DOMNode;
file_length number;
block_size binary_integer;
l_rootnode_name varchar2(200);
l_status varchar2(1000);
l_recerrcode varchar2(1000);
l_failcount varchar2(200);
l_reccount varchar2(200);
l_name varchar2(1000);
l_comments varchar2(2000);
l_exists boolean;
function convertclobtoxmlelement(p_document in clob)
return xmldom.DOMElement is
x_commitelement xmldom.DOMElement;
l_parser xmlparser.Parser;
begin
l_parser := xmlparser.newParser;
xmlparser.parseClob(l_parser,p_document);
x_commitelement := xmldom.getDocumentElement(xmlparser.getDocument(l_parser));
return x_commitelement;
end convertclobtoxmlelement;
begin
utl_file.fgetattr('XML_DIR',
'2.xml',
l_exists,
file_length,
block_size);
if not l_exists then
dbms_output.put_line('XML File not exist!!!');
return;
end if;
l_bfile := bfilename('XML_DIR','2.xml');
dbms_lob.createtemporary(l_clob,true);
dbms_lob.open(l_bfile,dbms_lob.lob_readonly);
dbms_lob.loadclobfromfile(l_clob,
l_bfile,
p_max_size,
dst_offset,
src_offset,
default_csid,
lang_ctx,
warning);
l_file_number := dbms_lob.fileexists(l_bfile);
if l_file_number = 0 then
dbms_output.put_line('XML File Convert Failed!!!');
return;
end if;
dbms_lob.close(l_bfile);
l_parser := dbms_xmlparser.newParser;
begin
dbms_xmlparser.parseClob(l_parser,l_clob);
exception
when others then
dbms_output.put_Line('XML File Not Full!!!');
return;
end;
l_doc := dbms_xmlparser.getDocument(l_parser);
dbms_lob.freetemporary(l_clob);
rootnode := xmldom.makeNode(xmldom.getDocumentElement(xmlparser.getDocument(l_parser)));
l_rootnode_name := xmldom.getNodeName(rootnode);
dbms_output.put_line('The root node name of the XML File is :' ||
l_rootnode_name);
dbms_xslprocessor.valueOf(rootnode,'RecCount/text()',l_reccount);
dbms_xslprocessor.valueOf(rootnode,'FailCount/text()',l_Failcount);
dbms_output.put_line('The name of the Current Node in The File is : ' ||
l_rootnode_name ||
'''s elements RecCount,FailCount Value is :' ||
l_reccount || ',' || l_Failcount);
l_status := xmldom.getAttribute(xmldom.makeElement(rootnode),'Status');
dbms_output.put_line('The name of the Current Node in The File is : ' ||
l_rootnode_name || '''s elements Status Value is :' ||
l_status);
l_nl := dbms_xmldom.getElementsByTagName(l_doc,'Item');
l_count := dbms_xmldom.getLength(l_nl);
for cur_emp in 0 .. dbms_xmldom.getLength(l_nl) - 1 loop
l_n := dbms_xmldom.item(l_nl,cur_emp);
dbms_xslprocessor.valueOf(l_n,'Name/text()',l_name);
dbms_xslprocessor.valueOf(l_n,'Comment/text()',l_comments);
parent_rootnode := dbms_xmldom.getParentNode(l_n);
l_rootnode_name := xmldom.getNodeName(parent_rootnode);
l_recerrcode := xmldom.getAttribute(xmldom.makeElement(parent_rootnode),
'RecErrCode');
dbms_output.put_line('Name :' || l_name || ',Comment = ' ||
l_comments || ',RecErrCode = ' || l_recerrcode);
end loop;
dbms_xmlparser.freeParser(l_parser);
dbms_xmldom.freeDocument(l_doc);
exception
when others then
dbms_lob.freetemporary(l_clob);
dbms_xmlparser.freeParser(l_parser);
dbms_xmldom.freeDocument(l_doc);
end;
2.xml
<?xml version="1.0" encoding="UTF-8"?> <Dfile Status="3"> <RecCount>200</RecCount> <FailCount>1</FailCount> <FailInfo> <FItem RecErrCode="2901"> <Item> <Name>test1</Name> <Comment>ssss</Comment> </Item> <Item> <Name>test2</Name> <Comment>llll</Comment> </Item> </FItem> </FailInfo> <FailInfo> <FItem RecErrCode="2902"> <Item> <Name>test3</Name> <Comment>sssssdfdfss</Comment> </Item> <Item> <Name>test4</Name> <Comment>llll</Comment> </Item> </FItem> </FailInfo> </Dfile>