我有一个如下的 XML 格式。
<ROWSET>
<ROW NUM="1">
<pid>1</pid>
<lon>25.3032016</lon>
<lat>54.7146368</lat>
<dt>20/10/2013 08:45:42 PM</dt>
</ROW>
<ROW NUM="2">
<pid>1</pid>
<lon>25.3032016</lon>
<lat>54.7146368</lat>
<dt>20/10/2013 08:45:47 PM</dt>
</ROW>
<ROW NUM="3">
<pid>1</pid>
<lon>25.3032016</lon>
<lat>54.7146368</lat>
<dt>20/10/2013 08:45:52 PM</dt>
</ROW>
<ROW NUM="4">
<pid>1</pid>
<lon>25.3032016</lon>
<lat>54.7146368</lat>
<dt>20/10/2013 08:45:57 PM</dt>
</ROW>
<ROW NUM="5">
<pid>1</pid>
<lon>2512.3032016</lon> --Error part (as the longitude fields only stores NUMBER (13,10))
<lat>54.7146368</lat>
<dt>20/10/2013 08:45:57 PM</dt>
</ROW>
</ROWSET>
到目前为止,我有以下程序将 XML 加载到表中,
PROCEDURE load_xml(i_xml CLOB)
AS
l_sqlerrm VARCHAR2(200);
BEGIN
INSERT INTO xml_stage (pid, longitude, latitude, date_time)
(SELECT EXTRACTVALUE(VALUE(row_list), '//pid') AS pid
,EXTRACTVALUE(VALUE(row_list), '//lon') AS lon
,EXTRACTVALUE(VALUE(row_list), '//lat') AS lat
,EXTRACTVALUE(VALUE(row_list), '//dt') AS dt
FROM TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(i_xml), '/ROWSET/ROW'))) row_list
);
COMMIT;
EXCEPTION WHEN OTHERS THEN
FOR rec IN
(SELECT EXTRACTVALUE(VALUE(row_list), '//pid') AS pid
,EXTRACTVALUE(VALUE(row_list), '//lon') AS lon
,EXTRACTVALUE(VALUE(row_list), '//lat') AS lat
,EXTRACTVALUE(VALUE(row_list), '//dt') AS dt
FROM TABLE(XMLSEQUENCE(EXTRACT(XMLTYPE(i_xml), '/ROWSET/ROW'))) row_list
)
LOOP
BEGIN
INSERT INTO xml_stage (pid, longitude, latitude, date_time)
VALUES (rec.pid, rec.lon, rec.lat, rec.dt);
EXCEPTION WHEN dup_val_on_index THEN
NULL;
WHEN others THEN
l_sqlerrm := substr(SQLERRM,1,200);
INSERT INTO errored_xml (error_id, date_time, error_desc, xmltext)
VALUES (load_stage_error_seq.nextval, SYSDATE, l_sqlerrm , i_xml);
COMMIT;
END;
END loop;
END load_xml;
我的问题是,如果出现任何异常,我目前正在将完整的 xml ( i_xml
) 记录到表中。errored_xml
有没有办法记录引发错误的特定 XML 行属性?
我的表结构如下,
CREATE TABLE xml_stage(
pid NUMBER(10),
longitude NUMBER (13,10) ,
latitude NUMBER (13,10) ,
date_time VARCHAR2(30),
CONSTRAINT xml_stage_pk PRIMARY KEY (pid,date_time)
);
CREATE TABLE errored_xml(
error_id NUMBER(6),
date_time DATE,
error_desc VARCHAR2(200),
xmltext CLOB,
CONSTRAINT errored_xml_pk PRIMARY KEY (error_id)
);
提前致谢。