oracle替换xml项目,点评在"ORACLE"中进行XML数据转换小项目[问题篇和答案篇]

a4c26d1e5885305701be709a3d33442f.png

在oracle中的pl/sql中建数据表

CONNECT SCOTT/TIGER;

DROP TABLE PEOPLE CASCADE CONSTRAINTS;

CREATE TABLE PEOPLE

(

PERSONID VARCHAR2(10) PRIMARY

KEY,

NAME VARCHAR2(20),

ADDRESS VARCHAR2(60),

TEL VARCHAR2(20),

FAX VARCHAR2(20),

EMAIL VARCHAR2(40)

);

people.xml文件内容

a4c26d1e5885305701be709a3d33442f.png

将people.xml中的数据插入到数据表中people中的存储过程写法

-- 解析 XML people.xml 文件的内容并保存到数据库表 PEOPLE 中

-- 以 SCOTT 用户登录 ORACLE 系统

-- 请先根据 xml 文件的内容创建 PEOPLE 表

--

set serveroutput on;

set echo on;

create or replace procedure

xml2table(dir varchar2, inpfile varchar2,

errfile varchar2) is

-- 参数说明:

--

dir 基本目录,如 'd:\xml\plsql'

-- inpfile 输入文件名,不含路径,如 'people.xml'

-- errfile 错误日志文件,保存解析错误信息, 如

'err.log'

p xmlparser.parser;

doc xmldom.DOMDocument;

-- 读取并处理文档元素

procedure readElements(doc xmldom.DOMDocument) is

nl xmldom.DOMNodeList;

len number;

n xmldom.DOMNode;

attn xmldom.DOMNode;

nnm xmldom.DOMNamedNodeMap;

nl2 xmldom.DOMNodeList;

len2 number;

strSQL varchar2(1000);

begin

-- 读取 PERSON 元素

nl :=

xmldom.getElementsByTagName_r(doc, 'PERSON');

len :=

xmldom.getLength(nl);

-- 遍历元素

for i in 0..len-1 loop

-- 构造动态 SQL 语句

strSQL := 'INSERT INTO PEOPLE VALUES (';

n := xmldom.item(nl, i);

if xmldom.getNodeName(n)='PERSON' then

nnm := xmldom.getAttributes(n); -- 读取 PERSONID

属性

attn := xmldom.item(nnm, 0);

strSQL := strSQL || '''' || xmldom.getNodeValue(attn) ||

'''';

end if;

-- 读取 PERSON 的子节点

nl2 := xmldom.getChildNodes(n);

len2 := xmldom.getLength(nl2);

-- 处理 NAME, ADDRESS, ... 等节点

for j in 0..len2-1 loop

n := xmldom.item(nl2, j);

strSQL := strSQL || ', ''' ||

xmldom.getNodeValue(xmldom.getFirstChild(n)) || '''';

end loop;

-- 完成 动态 SQL 语句的构造

strSQL := strSQL || ')';

-- dbms_output.put_line(strSQL);

-- 执行插入记录的 SQL 语句

execute immediate(strSQL); -- 执行动态 SQL

end loop;

commit; -- 提交插入

dbms_output.put_line('');

end readElements;

begin

-- 新建解析器实例

p :=

xmlparser.newParser;

-- 设置解析器特性

xmlparser.setValidationMode(p,

FALSE);

xmlparser.setErrorLog(p, dir

|| '\' || errfile);

xmlparser.setBaseDir(p,

dir);

-- 解析输入文件

xmlparser.parse(p, dir || '\'

|| inpfile);

-- 获取解析后的文档对象

doc :=

xmlparser.getDocument(p);

-- 读取文档元素

dbms_output.put_line('读取文档元素并保存到表 PEOPLE 中.');

readElements(doc);

-- 释放资源

xmldom.freeDocument(doc);

-- 处理异常

exception

when xmldom.INDEX_SIZE_ERR then

raise_application_error(-20120, 'Index Size error');

when xmldom.DOMSTRING_SIZE_ERR then

raise_application_error(-20120, 'String Size error');

when xmldom.HIERARCHY_REQUEST_ERR then

raise_application_error(-20120, 'Hierarchy request error');

when xmldom.WRONG_DOCUMENT_ERR then

raise_application_error(-20120, 'Wrong doc error');

when xmldom.INVALID_CHARACTER_ERR then

raise_application_error(-20120, 'Invalid Char error');

when xmldom.NO_DATA_ALLOWED_ERR then

raise_application_error(-20120, 'Nod data allowed error');

when xmldom.NO_MODIFICATION_ALLOWED_ERR

then

raise_application_error(-20120, 'No mod allowed error');

when xmldom.NOT_FOUND_ERR then

raise_application_error(-20120, 'Not found error');

when xmldom.NOT_SUPPORTED_ERR then

raise_application_error(-20120, 'Not supported error');

when xmldom.INUSE_ATTRIBUTE_ERR then

raise_application_error(-20120, 'In use attr error');

end xml2table;

/

show errors;

-- exec xml2table('d:\xml\plsql', 'people.xml',

'err.log');

将表中的数据插入到一个新的pople2.xml文件中

-- 将表 PEOPLE 中的记录导出到 XML 文档

-- 以 SCOTT 用户登录 ORACLE 系统

--

set serveroutput on;

set echo on;

create or replace procedure table2xml(outfile

varchar2 := 'd:\solutions\test.xml') is

doc xmldom.DOMDocument;

main_node xmldom.DOMNode;

root_node xmldom.DOMNode;

person_node xmldom.DOMNode;

item_node xmldom.DOMNode;

root_elmt xmldom.DOMElement;

item_elmt xmldom.DOMElement;

item_text xmldom.DOMText;

-- 定义选择记录的游标

CURSOR get_persons IS

select

personid, name, address, tel, fax, email

from

people;

begin

-- 创建文档对象

doc :=

xmldom.newDOMDocument;

-- 获得文档节点

main_node :=

xmldom.makeNode(doc);

-- 创建根元素

root_elmt :=

xmldom.createElement_x(doc, 'PEOPLE');

-- 向文档节点加入根节点

root_node :=

xmldom.a(main_node, xmldom.makeNode(root_elmt));

for

a_person in get_persons loop

-- 创建 PERSON 元素

item_elmt := xmldom.createElement_x(doc, 'PERSON');

-- 设置 PERSON 元素的 PERSONID 属性

xmldom.setAttribute(item_elmt, 'PERSONID',

a_person.personid);

-- 将 PERSON 元素加入根节点

person_node :=

xmldom.a(root_node,xmldom.makeNode(item_elmt));

-- 加入 NAME 节点

item_elmt := xmldom.createElement_x(doc, 'NAME');

item_node := xmldom.a(person_node,

xmldom.makeNode(item_elmt));

-- 加入文本节点

item_text := xmldom.createTextNode(doc, a_person.name);

item_node := xmldom.a(item_node,

xmldom.makeNode(item_text));

-- 加入 ADDRESS 节点

item_elmt := xmldom.createElement_x(doc, 'ADDRESS');

item_node := xmldom.a(person_node,

xmldom.makeNode(item_elmt));

-- 加入文本节点

item_text := xmldom.createTextNode(doc, a_person.address);

item_node := xmldom.a(item_node,

xmldom.makeNode(item_text));

-- 加入 TEL 节点

item_elmt := xmldom.createElement_x(doc, 'TEL');

item_node := xmldom.a(person_node,

xmldom.makeNode(item_elmt));

-- 加入文本节点

item_text := xmldom.createTextNode(doc, a_person.tel);

item_node := xmldom.a(item_node,

xmldom.makeNode(item_text));

-- 加入 FAX 节点

item_elmt := xmldom.createElement_x(doc, 'FAX');

item_node := xmldom.a(person_node,

xmldom.makeNode(item_elmt));

-- 加入文本节点

item_text := xmldom.createTextNode(doc, a_person.fax);

item_node := xmldom.a(item_node,

xmldom.makeNode(item_text));

-- 加入 EMAIL 节点

item_elmt := xmldom.createElement_x(doc, 'EMAIL');

item_node := xmldom.a(person_node,

xmldom.makeNode(item_elmt));

-- 加入文本节点

item_text := xmldom.createTextNode(doc, a_person.email);

item_node := xmldom.a(item_node, xmldom.makeNode(item_text));

end loop; -- for

-- 将文档对象保存到

外部文件中

xmldom.writeToFile(doc,

outfile);

--

释放资源

xmldom.freeDocument(doc);

-- 异常处理

exception

when xmldom.INDEX_SIZE_ERR then

raise_application_error(-20120, 'Index Size error');

when xmldom.DOMSTRING_SIZE_ERR then

raise_application_error(-20120, 'String Size error');

when xmldom.HIERARCHY_REQUEST_ERR then

raise_application_error(-20120, 'Hierarchy request error');

when xmldom.WRONG_DOCUMENT_ERR then

raise_application_error(-20120, 'Wrong doc error');

when xmldom.INVALID_CHARACTER_ERR then

raise_application_error(-20120, 'Invalid Char error');

when xmldom.NO_DATA_ALLOWED_ERR then

raise_application_error(-20120, 'Nod data allowed error');

when xmldom.NO_MODIFICATION_ALLOWED_ERR

then

raise_application_error(-20120, 'No mod allowed error');

when xmldom.NOT_FOUND_ERR then

raise_application_error(-20120, 'Not found error');

when xmldom.NOT_SUPPORTED_ERR then

raise_application_error(-20120, 'Not supported error');

when xmldom.INUSE_ATTRIBUTE_ERR then

raise_application_error(-20120, 'In use attr error');

end table2xml;

/

show errors;

-- exec table2xml('d:\xml\plsql\people2.xml');

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值