Oracle SQL生成XML到本地服务器

3vpjnl9f  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(117)

我有一个下面的列和基于这些列的表,我需要生成下面的XML有很多contant值。
PHX_BADGE、MODELTYPE、PROGRAMID、CONFIGURATIONID
发行日期:

  • 我需要自动生成本地服务器上获得的结果,但无法实现。
  • 此外,声明为XMLForest(PROGRAMID“value”,RESTIVE_DATE“startDate”)的RESTIVE DATE需要转换为时区,但代码我得到文字字符串错误。日期“2023-06- 01 T00:00:00-07:00所需的格式

有人可以指导我,我如何才能实现这一点。
注意:我需要为每行生成XML,总共有110行。
下面是我写的,它生成的结果的值为'(XMLTYPE'。我想看看这个编辑。

SELECT XMLElement("SDPSyncMessage", 
                  XMLAttributes('http://www.emeter.com/energyip/syncinterface/v8' AS
                                  "xmlns",'http://www.w3.org/2001/XMLSchema-instance' AS
                                  "xmlns:xsi",
                                'http://www.emeter.com/energyip/syncinterface/v8 UniversalSyncInterface.xsd' AS
                                  "xsi:schemaLocation"),
                  XMLElement("header",
                    XMLElement("verb",'create'),
                    XMLElement("noun",'SDPSync'),
                    XMLElement("revision",'1'),
                    XMLElement("dateTime",TO_TIMESTAMP_TZ(sysdate,'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')),
                    XMLElement("source",'CONV'),
                    XMLElement("messageID",'test01'),
                    XMLElement("syncMode",'sync'),
                    XMLElement("optimizationLevel",'Optimistic')                                      
                  ),                  
                XMLElement("payload",
                XMLElement("device",
                  XMLForest(phx_badge "mRID", name "deviceClass", modeltype "model"),
                  XMLElement("status",'Installed'),
                  XMLElement("type",'Meter'),
                  XMLElement("deviceFunctionType",'N'),
                  XMLElement("parameter",
                    XMLElement("name",'Configuration ID'),
                    XMLForest(CONFIGURATIONID "value", EFFECTIVE_DATE "startDate")),
                    XMLElement("parameter",
                    XMLElement("name",'Program ID'),
                    XMLForest(PROGRAMID "value", EFFECTIVE_DATE "startDate"))
                  ))) AS "RESULT"
   FROM ASSETS
   where phx_loc = '816840009'

Expected Output

<SDPSyncMessage xmlns="http://www.emeter.com/energyip/syncinterface/v8">
    <header>
        <verb>SDPSync</verb>    <!-- CONSTANT -->
        <noun>SDPSync</noun>    <!-- CONSTANT -->
        <revision>1</revision>  <!-- CONSTANT -->
        <dateTime>2023-08-21</dateTime>     <!-- sysdate -->
        <source>CIS</source>    <!-- CONSTANT -->
        <messageID>test</messageID>     <!-- CONSTANT -->
        <syncMode>Sync</syncMode>   <!-- CONSTANT -->
        <optimizationLevel>Optimistic</optimizationLevel>       <!-- CONSTANT -->
    </header>
    <payload>
        <device>
            <mRID>3608246</mRID>  <!-- PHX_BADGE -->
            <model>RXRSD</model>    <!-- MODELTYPE -->
            <status>Installed</status>  <!-- CONSTANT -->
            <type>Meter</type>  <!-- CONSTANT -->
            <parameter>
                <name>Configuration ID</name>   <!-- CONSTANT -->
                <value>001</value>  <!-- PHX_BADGE -->
                <startDate>2017-02-23 00:00:00</startDate>  <!-- EFFECTIVE_DATE -->
            </parameter>
            <parameter>
                <name>Program ID</name> <!-- CONSTANT -->
                <value>03</value>   <!-- PROGRAMID -->
                <startDate>2017-02-23 00:00:00</startDate>  <!-- EFFECTIVE_DATE -->
            </parameter>
        </device>       
    </payload>
</SDPSyncMessage>
bvjveswy

bvjveswy1#

问题是这一部分:

TO_TIMESTAMP_TZ(sysdate,'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')

SYSDATE返回DATE值。切勿在已经是TIMESTAMP(或DATE)的值上使用TO_TIMESTAMP(或TO_TIMESTAMP_TZTO_DATE
您需要将TIMESTAMP格式化为特定格式,请尝试以下操作:

XMLElement("dateTime", TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM'))

但是,在预期输出中,您要求<dateTime>2023-08-21</dateTime>,您可以使用以下命令:

XMLElement("dateTime", TO_CHAR(SYSTIMESTAMP, 'YYYY-MM-DD'))

保存文件的工作方式如下。首先创建一个过程来保存文件:

CREATE PROCEDURE SaveFile(
    FileContent IN OUT NOCOPY CLOB, 
    FolderName IN VARCHAR2, 
    FileName IN VARCHAR2, 
    UTF8 IN BOOLEAN DEFAULT TRUE) AS
        
    BUFFER VARCHAR2(4096 CHAR);
    OFFSET PLS_INTEGER := 1;
    FileLength INTEGER;
    amount PLS_INTEGER := 1024;
    fhandle UTL_FILE.FILE_TYPE;
    
BEGIN

    FileLength := DBMS_LOB.GETLENGTH(FileContent);
    IF UTF8 THEN
        fhandle := UTL_FILE.FOPEN_NCHAR(FolderName, FileName, open_mode => 'w', max_linesize => 32000);
    ELSE
        fhandle := UTL_FILE.FOPEN(FolderName, FileName, open_mode => 'w', max_linesize => 32000);
    END IF;
        
    LOOP
        EXIT WHEN OFFSET > FileLength;
        DBMS_LOB.READ(FileContent, amount, OFFSET, BUFFER);
        IF UTF8 THEN
            UTL_FILE.PUT_NCHAR(fhandle, BUFFER);
        ELSE
            UTL_FILE.PUT(fhandle, BUFFER);
        END IF;     
        UTL_FILE.FFLUSH(fhandle);
        OFFSET := OFFSET + amount;
    END LOOP;
    UTL_FILE.FCLOSE (fhandle);

END SaveFile;

你可以这样使用它:

CREATE OR REPLACE DIRECTORY XML_OUTPUT_FOLDER AS '/var/app/out';
GRANT EXECUTE, READ, WRITE ON DIRECTORY XML_OUTPUT_FOLDER TO ... ;

DECLARE
    xmlContent CLOB;
SELECT

    SELECT XMLELEMENT("SDPSyncMessage", 'foo').getClobVal() 
    INTO xmlContent
    FROM dual;

    Pmd_Util.SaveFile(xmlContent, 'XML_OUTPUT_FOLDER', 'bar.xml');
    DBMS_LOB.FREETEMPORARY(xmlContent);

END;

相关问题