3

我有一个如下的 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)
     );

提前致谢。

4

2 回答 2

2

XMLFOREST我自己使用like找到了答案,

XMLELEMENT("ROW",
         XMLFOREST( rec.pid as "pid"
                  , rec.lon as "lon"
                  , rec.lat as "lat" 
                  , rec.dt as "dt" ) 
       ).getClobVal()

这里我使用getClobVal()了,这是将 插入XML到包含CLOB数据类型的列中。

注意:一个问题XMLFOREST是,如果值为 null,则不会XML为其生成标签。在这里,如果值为空,我不需要生成标签。

于 2013-12-19T05:10:20.373 回答
2

您可以像这样插入errored_xml

INSERT INTO errored_xml
     (
          error_id,
          date_time,
          error_desc,
          XMLTEXT
     )
     VALUES
     (
          load_stage_error_seq.nextval,
          SYSDATE,
          l_sqlerrm ,
          XMLELEMENT ("ROW", XMLELEMENT("pid", rec.pid) , XMLELEMENT("lon", rec.lon), XMLELEMENT("lat", rec.lat), XMLELEMENT("dt", rec.dt))
     );

(你需要一个 varchar2 的 alter table 来存储这个 xml)

于 2013-10-23T07:08:26.673 回答