xml标记在中间中断

qojgxg4l  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(218)

我们有一个使用sql编写xml文件的过程,我们被要求提高它的性能。目前,它逐行打印xml,如下所示:

begin

    dbms_output.put_line('<tns:jpk>');

    -- header section
    for i in (select xmlelement("tns:header", header_num)
                    ,xmlelement("tns:customer", customer_name)
                    ,xmlelement("tns:po_number", po_number) li_xml
              from  header_tbl) loop

        dbms_output.put_line( i.li_xml.getclobval() );

        dbms_output.put_line('<tns:lines>');

        -- Lines section    
        for x in (select xmlelement("tns:line_num", line_num)
                    ,xmlelement("tns:order", order_dtl)
                    ,xmlelement("tns:qty", qty)
                  from  Lines_tbl) lx loop

            dbms_output.put_line( x.lx_xml.getclobval() );

            dbms_output.put_line('<tns:Summary>');

            -- Summary section  
            for y in (select xmlelement("tns:sum_num", sum_num)
                        ,xmlelement("tns:total_amt, total_amt)
                        ,xmlelement("tns:total_qty, total_qty)
                    from  Summary_Tbl) lc_xml loop

                dbms_output.put_line( y.lc_xml.getclobval() );

            end loop;

            dbms_output.put_line('<tns:Summary>');

        dbms_output.put_line('</tns:lines>');

        end loop;

    end loop;

    dbms_output.put_line('</tns:jpk>');

end;

上面的示例代码如下所示:

<tns:jpk>
    <tns:header>1</tns:header>
    <tns:customer>1000</tns:customer>
    <tns:po_number>909090</tns:po_number>
    <tns:lines>
        <tns:line_num>1</tns:line_num>
        <tns:order>Other FA Open Asset Cost Pozostale Srodki Trwale -Wartosc poczatkowa</tns:order>
        <tns:qty>1</tns:qty>
        <tns:Summary>
            <tns:sum_num>1</tns:sum_num>
            <tns:total_amt>1000</tns:total_amt>
            <tns:total_qty>1</tns:total_qty>
        </tns:Summary>
    </tns:lines>
</tns:jpk>

我使用了一种不同的方法,比如下面的代码,设法使它更快一些:

declare 

    xml_c xmltype;

    procedure print_clob( p_clob in clob ) is
        v_offset number := 1;
        v_chunk_size number := 10000;
        --v_chunk_size number := 32767;
      begin
        loop
          exit when v_offset > dbms_lob.getlength(p_clob);
          dbms_output.put_line( dbms_lob.substr( p_clob, v_chunk_size, v_offset ) );

          v_offset := v_offset + v_chunk_size;
        end loop;

     EXCEPTION
        WHEN OTHERS THEN
            dbms_output.put_line('print_clob. others ' || SQLERRM);
    end print_clob;   

begin

    select  xmlagg(xmlconcat("<tns:jpk>",
        -- header section
        (select xmlagg(xmlconcat(
             xmlelement("tns:header", header_num)
            ,xmlelement("tns:customer", customer_name)
            ,xmlelement("tns:po_number", po_number)
            ,xmlelement("tns:lines", 
            -- lines section
            ,(select 
                xmlagg(xmlconcat(
                    xmlelement("tns:line_num", line_num)
                ,   xmlelement("tns:order", order_dtl)
                ,   xmlelement("tns:qty", qty)
                ,   (select 
                        xmlagg(xmlconcat(
                            xmlelement("tns:sum_num", sum_num)
                        ,   xmlelement("tns:total_amt", total_amt)
                        ,   xmlelement("tns:total_qty", total_qty)))
                    from  Summary_Tbl)
                ))
                from  Lines_tbl)
                )
            ))
        from    header_tbl)
    ))
    into    xml_c
    from    dual;

    print_clob( xml_c.getclobval );

end;

上面的代码生成了一个非常长的xml,并将其切块并打印出来。根据执行时间,它工作良好,速度更快。但是,每当有一段很长的字符串时,输出有时会如下所示:

<tns:jpk>
    <tns:header>1</tns:header>
    <tns:customer>1000</tns:customer>
    <tns:po_number>909090</tns:po_number>
    <tns:lines>
        <tns:line_num>1</tns:line_num>
        <tns:order>Other FA Open Asset Cost Pozostale 
Srodki Trwale -Wartosc poczatkowa</tns:order>
        <tns:qty>1</tns:qty>
        <tns:Summary>
            <tns:sum_num>1</tns:sum_num>
            <tns:total_amt>1000</tns:total_amt>
            <tns:total_qty>1</tns:total_qty>
        </tns:Summary>
    </tns:lines>
</tns:jpk>

有没有办法让我在打印前找出标签及其内容是否会超出限制?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题