PL/SQL 将对象类型转换为 XML 日期不是 ISO8601
PL/SQL Transforming Object Type to XML Date is not ISO8601
我从对象转换为 XML 时遇到日期格式问题,因为它不是 ISO8601
示例:
CREATE OR REPLACE TYPE APPS.MYOBJECT IS OBJECT (
MYDATE_1 DATE,
MYDATE_2 DATE);
DECLARE
v_myobject MYOBJECT;
v_xml XMLTYPE;
BEGIN
v_myobject:=new MYOBJECT(SYSDATE,SYSDATE);
v_xml:=XMLTYPE(v_myobject);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
这个输出是:
XML=<MYOBJECT><MYDATE_1>19-MAR-15</MYDATE_1><MYDATE_2>19-MAR-15</MYDATE_2></MYOBJECT>
我想要 ISO8601 格式的日期,如下所示:
XML=<MYOBJECT><MYDATE_1>2015-02-19T15:16:00Z</MYDATE_1><MYDATE_2>2015-02-19T15:16:00Z</MYDATE_2></MYOBJECT>
有什么方法可以指定数据类型 DATE 的转换格式???
谢谢
不理想,但作为解决方法,您可以更改 NLS_DATE_FORMAT:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
set serveroutput on
DECLARE
v_myobject MYOBJECT;
v_xml XMLTYPE;
BEGIN
v_myobject:=new MYOBJECT(SYSDATE,SYSDATE);
v_xml:=XMLTYPE(v_myobject);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
/
Session altered.
anonymous block completed
XML=<MYOBJECT><MYDATE_1>2015-03-19 18:42:23</MYDATE_1><MYDATE_2>2015-03-19 18:42:23</MYDATE_2></MYOBJECT>
如果需要,可以包含固定的 T 和 Z,方法是改用它:
alter session set nls_date_format = 'YYYY-MM-DD"T"HH24:MI:SS"Z"';
或dbms_xmlquery
seems to handle dates closer to what you expect,所以你可以这样做:
-- just to check we are not just picking up the previous value!
alter session set nls_date_format = 'DD/MM/YYYY';
DECLARE
v_xml XMLTYPE;
v_ctx dbms_xmlquery.ctxhandle;
BEGIN
v_ctx := dbms_xmlquery.newcontext('select MYOBJECT(SYSDATE,SYSDATE) MYOBJECT from dual');
dbms_xmlquery.setrowtag(v_ctx, null);
dbms_xmlquery.setrowsettag(v_ctx, null);
v_xml := XMLTYPE(dbms_xmlquery.getxml(v_ctx));
dbms_xmlquery.closecontext (v_ctx);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
/
Session altered.
anonymous block completed
XML=<?xml version = '1.0'?>
<MYOBJECT>
<MYDATE_1>2015-03-19 19:07:41</MYDATE_1>
<MYDATE_2>2015-03-19 19:07:41</MYDATE_2>
</MYOBJECT>
但这假设您不需要该对象可用于块中的任何其他内容。如果这些很重要,则没有 T 和 Z。
我从对象转换为 XML 时遇到日期格式问题,因为它不是 ISO8601
示例:
CREATE OR REPLACE TYPE APPS.MYOBJECT IS OBJECT (
MYDATE_1 DATE,
MYDATE_2 DATE);
DECLARE
v_myobject MYOBJECT;
v_xml XMLTYPE;
BEGIN
v_myobject:=new MYOBJECT(SYSDATE,SYSDATE);
v_xml:=XMLTYPE(v_myobject);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
这个输出是:
XML=<MYOBJECT><MYDATE_1>19-MAR-15</MYDATE_1><MYDATE_2>19-MAR-15</MYDATE_2></MYOBJECT>
我想要 ISO8601 格式的日期,如下所示:
XML=<MYOBJECT><MYDATE_1>2015-02-19T15:16:00Z</MYDATE_1><MYDATE_2>2015-02-19T15:16:00Z</MYDATE_2></MYOBJECT>
有什么方法可以指定数据类型 DATE 的转换格式???
谢谢
不理想,但作为解决方法,您可以更改 NLS_DATE_FORMAT:
alter session set nls_date_format = 'YYYY-MM-DD HH24:MI:SS';
set serveroutput on
DECLARE
v_myobject MYOBJECT;
v_xml XMLTYPE;
BEGIN
v_myobject:=new MYOBJECT(SYSDATE,SYSDATE);
v_xml:=XMLTYPE(v_myobject);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
/
Session altered.
anonymous block completed
XML=<MYOBJECT><MYDATE_1>2015-03-19 18:42:23</MYDATE_1><MYDATE_2>2015-03-19 18:42:23</MYDATE_2></MYOBJECT>
如果需要,可以包含固定的 T 和 Z,方法是改用它:
alter session set nls_date_format = 'YYYY-MM-DD"T"HH24:MI:SS"Z"';
或dbms_xmlquery
seems to handle dates closer to what you expect,所以你可以这样做:
-- just to check we are not just picking up the previous value!
alter session set nls_date_format = 'DD/MM/YYYY';
DECLARE
v_xml XMLTYPE;
v_ctx dbms_xmlquery.ctxhandle;
BEGIN
v_ctx := dbms_xmlquery.newcontext('select MYOBJECT(SYSDATE,SYSDATE) MYOBJECT from dual');
dbms_xmlquery.setrowtag(v_ctx, null);
dbms_xmlquery.setrowsettag(v_ctx, null);
v_xml := XMLTYPE(dbms_xmlquery.getxml(v_ctx));
dbms_xmlquery.closecontext (v_ctx);
DBMS_OUTPUT.put_line('XML='||v_xml.getstringval);
END;
/
Session altered.
anonymous block completed
XML=<?xml version = '1.0'?>
<MYOBJECT>
<MYDATE_1>2015-03-19 19:07:41</MYDATE_1>
<MYDATE_2>2015-03-19 19:07:41</MYDATE_2>
</MYOBJECT>
但这假设您不需要该对象可用于块中的任何其他内容。如果这些很重要,则没有 T 和 Z。