--------------------------------------------配置----------------------------------------------------
--创建控制列表并赋权
BEGINEND;
-------------------------------------------------------------代码--------------------------------------------------------------
create or replace procedure proc_recive_xml (p_url in varchar2)
/***
* function:通过http请求xml数据,解析接收的xml数据,并入库
* author:
* date:
*/
is
l_http_request utl_http.req;
l_http_response utl_http.resp;
l_clob clob;
l_text varchar2(32767);
l_parser dbms_xmlparser.parser;
l_doc dbms_xmldom.domdocument;
l_nl dbms_xmldom.domnodelist;
l_n dbms_xmldom.domnode;
parent_rootnode dbms_xmldom.domnode;
l_rootnode_name varchar2(200);
l_name varchar2(1000);
l_code varchar2(6);
type v_index_stock is record (
index_name varchar2(50),--板块名称
stock_code varchar2(6)--股票代码
);
type v_arr_index_stock is table of v_index_stock index by binary_integer;
arr_index_stock v_arr_index_stock;
begin
/*使用clob接收http数据*/
dbms_lob.createtemporary(l_clob, false);-- 初始化clob
-- 发送http请求
l_http_request := utl_http.begin_request(p_url);
utl_http.set_header(l_http_request, 'content-type', 'text/html; charset=utf8');
l_http_response := utl_http.get_response(l_http_request);
-- 存储接收的http数据到clob
begin
loop
utl_http.read_text(l_http_response, l_text, 32767);
dbms_lob.writeappend (l_clob, length(l_text), l_text);
end loop;
exception
when utl_http.end_of_body then
utl_http.end_response(l_http_response);
end;
/*使用dom解析xml*/
l_parser := dbms_xmlparser.newparser;-- 创建解析器
-- 解析并创建dom文档
dbms_xmlparser.parseclob(l_parser, l_clob);
l_doc := dbms_xmlparser.getdocument(l_parser);
--取节点item下各元素的值,先将items节点全部存放在 l_nl中
l_nl := dbms_xmldom.getelementsbytagname(l_doc,'stock');
for cur_emp in 0 .. dbms_xmldom.getlength(l_nl) - 1 loop
l_n := dbms_xmldom.item(l_nl, cur_emp);
-- 得到节点stock下元素的值
l_code := xmldom.getattribute(xmldom.makeelement(l_n),'code');
-- 得到节点stock的父节点bk
parent_rootnode := dbms_xmldom.getparentnode(l_n);
l_rootnode_name := xmldom.getnodename(parent_rootnode);
-- 得到节点bk的属性name的值
l_name := xmldom.getattribute(xmldom.makeelement(parent_rootnode),'name');
arr_index_stock(arr_index_stock.count + 1).index_name := l_name;
arr_index_stock(arr_index_stock.count).stock_code := l_code;
end loop;
-- 释放分析函数的资源
dbms_xmlparser.freeparser(l_parser);
-- 将doc清空,释放资源
dbms_xmldom.freedocument(l_doc);
--清空数据
delete from tb_index_0292;
for idx in 1..arr_index_stock.count loop
--dbms_output.put_line('name =' ||arr_stock_concept(idx).index_name || ' ,code = '||arr_stock_concept(idx).stock_code);
insert into tb_index_0292
values(idx,arr_index_stock(idx).index_name,arr_index_stock(idx).stock_code,ob_seq_id.nextval);
end loop;
commit;
--释放临时clob
dbms_lob.freetemporary(l_clob);
exception
when others then
rollback;
utl_http.end_response(l_http_response);
dbms_lob.freetemporary(l_clob);
end;
-------------------------------------------------文件格式------------------------------------------------------------