ORA-22806:生成XML输出时不是对象或REF:错误

时间:2013-03-05 16:05:22

标签: xml oracle

我正在尝试通过创建过程来创建XML输出文件,并将该过程用于并发请求。请找到以下包裹程序代码。

并发程序

出错了
  

“原因:由于ORA-19202导致FDPSTP失败:XML处理ORA-22806发生错误:不是对象或REF”。

CREATE OR REPLACE PACKAGE BODY xx_outbound_xml1
AS
   PROCEDURE xx_insert_outbound_xml1 (
      errbuff   OUT   VARCHAR2,
      errcode   OUT   NUMBER
   )
   IS
      ln_ctx        DBMS_XMLGEN.ctxhandle;
      lc_xml_clob   CLOB;
   BEGIN
      ln_ctx :=
         DBMS_XMLGEN.newcontext
            (   'SELECT XMLELEMENT("litleRequest",
                              XMLATTRIBUTES('
             || 'to_char(sysdate,''DDMMYYYHH24MI:SS'')'
             || ' AS "Id"                   ),
                (SELECT 
                    XMLAGG(
                        XMLELEMENT(
                        evalname(Decode((select order_type FROM my_orders WHERE order_id = mo.order_id),''CO'',''"Credit"'',''"Sale"'')).getstringval(),
                        XMLFOREST (
                        mo.order_id AS "orderId",
                        mo.order_type as "orderType"
                                    )
                                )
                        )
  FROM my_orders mo
                )
            ) xml
 FROM dual'
            );
      DBMS_XMLGEN.setrowsettag (ln_ctx, NULL);
      DBMS_XMLGEN.setrowtag (ln_ctx, NULL);
      --DBMS_XMLGEN.setNullHandling(ln_ctx,dbms_xmlgen.EMPTY_TAG);
      lc_xml_clob := DBMS_XMLGEN.getxml (ln_ctx);
      lc_xml_clob := REPLACE (lc_xml_clob, '<XML>', '');
      lc_xml_clob := REPLACE (lc_xml_clob, '</XML>', '');
      DBMS_XSLPROCESSOR.clob2file (lc_xml_clob,
                                   'INBOUNDFOLDER',
                                   'output.xml',
                                   0
                                  );
      DBMS_LOB.freetemporary (lc_xml_clob);
   END xx_insert_outbound_xml1;
END xx_outbound_xml1;

如果订单类型为CO,则tag应为Credit,或者如果order_type为SO,则tag应为Sales。

我的输出应该是:

Output

1 个答案:

答案 0 :(得分:2)

您不需要将DBMS_XMLGEN与XML函数一起使用,因为它已经是XML!此错误也来自于您尝试在非getstringval对象上调用XMLType这一事实。

而不是这样做:

create or replace package body xx_outbound_xml1
as
   procedure xx_insert_outbound_xml1 (
      errbuff   out   varchar2,
      errcode   out   number
   )
   is
      ln_ctx        dbms_xmlgen.ctxhandle;
      lc_xml_clob   clob;
   begin
     select xmlserialize(document
              xmlelement(
                "litleRequest",
                xmlattributes(to_char(sysdate,'DDMMYYYHH24MI:SS')  AS "Id"),
                xmlagg(
                  xmlelement(
                    evalname(Decode(order_type, 'CO','Credit','Sale')),
                    xmlforest (
                      mo.order_id AS "orderId",
                      mo.order_type as "orderType"
                    )
                  )
                )
              )
            ) xml
     into lc_xml_clob
     from my_orders mo;

    dbms_xslprocessor.clob2file (lc_xml_clob,
                                 'INBOUNDFOLDER',
                                 'output.xml',
                                 0
                                );
   end xx_insert_outbound_xml1;
end xx_outbound_xml1;
/

例如使用该代码(加上我在xmlserialize中添加“缩进”,以便更容易在此处看到):

SQL> var b varchar2(2000)
SQL> var c number
SQL> exec xx_outbound_xml1.xx_insert_outbound_xml1(:b, :c)

PL/SQL procedure successfully completed.

SQL> host cat /tmp/output.xml
<litleRequest Id="05030131727:20">
  <Sale>
    <orderId>1132339612</orderId>
    <orderType>SO</orderType>
  </Sale>
  <Credit>
    <orderId>1132339530</orderId>
    <orderType>CO</orderType>
  </Credit>
  <Sale>
    <orderId>1132339116</orderId>
    <orderType>SO</orderType>
  </Sale>
  <Credit>
    <orderId>1132338886</orderId>
    <orderType>CO</orderType>
  </Credit>
</litleRequest>

SQL> select * from my_orders;

  ORDER_ID OR
---------- --
1132339612 SO
1132339530 CO
1132339116 SO
1132338886 CO

SQL>