简体   繁体   English

SQL 将 blob 字段转换为 XML 的存储过程

[英]SQL stored procedure to convert a blob field to XML

I have an xml file stored in the DB.我有一个 xml 文件存储在数据库中。 this xml file is converted to a zipped byte[] and then stored in the db as shown below:这个 xml 文件被转换为一个压缩的 byte[],然后存储在数据库中,如下所示:

 ByteArrayOutputStream byteOut = null;
 ZipOutputStream out = null;
 try {
     byteOut = new ByteArrayOutputStream();
     out = new ZipOutputStream(byteOut);
     out.setLevel(level);      
      ZipInput entry = new ZipStringInput("string", xmlString));
      out.putNextEntry(getZipEntry(encrypt, entry.getEntryName()));
        InputStream in = entry.getInputStream();
        byte[] buffer = new byte[BUFFER];
        for (int length; (length = in.read(buffer)) != -1; ) {
            out.write(buffer, 0, length);
        }
     out.finish();
     saveToDB(byteOut.toByteArray()

In java I can easily fetch it like shown below (simplified wihtout catch, close etc.,):在 java 中,我可以很容易地获取它,如下所示(简化后没有捕获、关闭等):

byte[] xmlBytes = (byte[])blob;
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ByteArrayInputStream byteIn = new ByteArrayInputStream(xmlBytes );) {
    try (ZipInputStream zipIn = new ZipInputStream(byteIn);) {          
        for (ZipEntry entry; (entry = zipIn.getNextEntry()) != null;) {
            byte[] buffer = new byte[32768];
            for (int length; (length = zipIn.read(buffer, 0, buffer.length)) != -1;) {
                out.write(buffer, 0, length);                       
            }
        }
        return out.toString("UTF-8"));
}

Oracle DB: Now I have to task to do the same in a SQL stored procedure. Oracle DB:现在我必须在 SQL 存储过程中执行相同的任务。 how do I do it.我该怎么做。 I have tried combinations of lz_uncompress, lob.substr etc., I can see the bytes, but not sure how to convert it to the XML file.我试过 lz_uncompress、lob.substr 等的组合,我可以看到字节,但不确定如何将其转换为 XML 文件。

CREATE OR REPLACE PROCEDURE print_clob AS
v_clob    CLOB;
v_varchar VARCHAR2(32767);
v_start   PLS_INTEGER := 1;
v_buffer  PLS_INTEGER := 32767;
blob_in   BLOB;
x         XMLTYPE;
BEGIN
    dbms_lob.createtemporary(v_clob, true);
    FOR i IN (
        SELECT
            blobdata AS blob_in
        FROM
            myTable
        WHERE
            id = 123
    ) LOOP
        FOR j IN 1..ceil(dbms_lob.getlength(i.blob_in) / v_buffer) LOOP
            v_varchar := dbms_lob.substr(i.blob_in, v_buffer, v_start);                 
            dbms_lob.writeappend(v_clob, length(v_varchar), v_varchar);
            v_start := v_start + v_buffer;
        END LOOP; 
        dbms_output.put_line(v_clob ); prints some binary stirng looking like '504B0304140008000800E0843E4200000000000000000.....'
      --  dbms_output.put_line(UTL_RAW.CAST_TO_NVARCHAR2(v_clob )); -> throws  error
      --  dbms_output.put_line(utl_compress.lz_uncompress(v_clob )); -> throws  error  
        --x := xmltype.createxml(v_clob); -> Throws error that the input it not an XML <, but some randoms tring
     --   dbms_output.put_line(x.getclobval());
    END LOOP;
END;

You are trying to substring a BLOB and stick it into a CLOB without any conversion.您正在尝试 substring 一个 BLOB 并将其粘贴到 CLOB 中而不进行任何转换。 If the BLOB is compressed, you'll need to uncompress it first with utl_compress.lz_uncompress .如果 BLOB 是压缩的,您需要先使用utl_compress.lz_uncompress解压缩它。 Then once uncompressed, you need to convert it to a CLOB using dbms_lob.converttoclob .然后一旦解压缩,您需要使用dbms_lob.converttoclob将其转换为 CLOB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM