【问题标题】:how to create an xml file for each record如何为每条记录创建一个xml文件
【发布时间】:2014-11-21 19:49:34
【问题描述】:

我有一个名为 test 的表,它有 n 个列,包括一个标志列。现在我的问题是,如何为每条记录创建一个 xml 文件,同时我还需要更新标志列。

考虑下表

sno 详细信息标志

1,'xyz','y' 2,'abc','y' 3,'def','y' 4,'ghi','y' 5,'jkl','y' 6,'mno','y'

现在我想为记录 1、'xyz'、'y' 创建 xml 文件,比如 xmlfile1,并且需要将表中的标志状态更新为 'n',我需要对所有其他记录执行同样的操作.

任何人都可以帮助我吗?

提前致谢。

【问题讨论】:

标签: xml oracle plsql


【解决方案1】:

试试这个

with xml_source as (select 1 id, 'xyz' val, 'y' flag
                      from dual
                    union all
                    select 2, 'abc', 'y'
                      from dual
                    union all
                    select 3, 'def', 'y'
                      from dual
                    union all
                    select 4, 'ghi', 'y'
                      from dual
                    union all
                    select 5, 'jkl', 'y'
                      from dual
                    union all
                    select 6, 'mno', 'y' from dual)

 select xmlagg(xmlelement(TABLE_NAME, (xmlelement(TABLE_ROW, XMLATTRIBUTES(x.id AS ID), xmlforest(x.val as Value)))))
  from xml_source x

您还可以阅读Generating XML Data from the Database

【讨论】:

  • 嗨 Mikron Tnx 它工作正常..,但我想要将生成的文件复制到一个目录并将标志列值更新为“n”的过程
  • 写生成的xml,看看utl_file package,导出成功后,你可以根据上面查询中选择的行,为你的表创建一个update。跨度>
【解决方案2】:

感谢您的建议..我按照下面的方式完成了

create table test(sno number,details varchar2(20),flag varchar2(1) 约束flag_cons check(flag in('y','n')));

插入测试值(1,'xyz','y');

插入测试值(2,'abc','y');

插入测试值(3,'def','y');

插入测试值(4,'ghi','y');

插入测试值(5,'jkl','y');

插入测试值(6,'mno','y');

创建或替换 DIRECTORY xml_dir 为 xxxxx;

创建或替换过程 xml_example 是

CURSOR query_str is select * from test ;


row_type test%rowtype;

xml_file clob;

v_MyFileHandle UTL_FILE.FILE_TYPE;

开始 对于 query_str 中的 row_type 循环

     select dbms_xmlquery.getxml('select * from test where sno='||row_type.sno,0) into xml_file from dual;

     v_MyFileHandle := UTL_FILE.FOPEN('XML_DIR','xmlfile'||to_char(query_str%rowcount),'w');

     UTL_FILE.PUT_LINE(v_MyFileHandle,xml_file  );

     UTL_FILE.FCLOSE(v_MyFileHandle);

     update test set flag='n' where sno=row_type.sno ;

END LOOP;

结束 xml_example;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多