我是XML新手,但已经被赋予了将一组100,000条记录输出到XML文件的任务,这将是一个更大的文件。在每个记录中,还有子数据子集也需要输出到文件中(参见下面的代码结构)。
我试图找到这个dbms_xmldom的最佳解决方案,但是看看论坛这是一个过时的工具,但我知道有XMLElement但我认为这只适用于SQL而不是PL / SQL。
除了dbms_xmldom以外,PL / SQL中是否还有其他功能可以使用,或者我应该坚持我正在做的事情。该工具必须将每个记录输出到输出文件。
当前版本的数据库是11g版本1.
任何建议都会提前感谢。
BEGIN
For r_prod_rec IN c_prod_rec
LOOP
output process master record to XML file
For r_prod_child1_rec IN c_prod_child1_rec(r_prod_rec.id)
LOOP
output process child record from table1 to XML file
END LOOP
For r_prod_child2_rec IN c_prod_child2_rec(r_prod_rec.id)
LOOP
output process child record table2 to XML file
END LOOP
END LOOP
END
答案 0 :(得分:0)
Oracle包有DBMS_XMLGEN包。您可以使用它将查询结果转换为xmltype。在我的例子中,我也使用" CURSOR表达式"。
declare
v_xml xmltype;
begin
v_xml := DBMS_XMLGEN.GETXMLTYPE ('select cursor(select x.COLUMN_NAME,x.DATA_TYPE from user_tab_columns x where x.table_name = y.table_name),y.table_name from user_tables y where rownum < 3');
dbms_output.put_line(v_xml.getClobVal());
end;
答案 1 :(得分:0)
Oracle安装程序:
CREATE TABLE parents ( ID, Attr1, Attr2 ) AS
SELECT LEVEL, 'A.' || LEVEL, 'B.' || LEVEL
FROM DUAL
CONNECT BY LEVEL <= 5;
CREATE TABLE children ( ID, Attr1, Parent_ID ) AS
SELECT LEVEL, 'C.' || LEVEL, CASE WHEN LEVEL <= 3 THEN 1
WHEN LEVEL <= 6 THEN 2
WHEN LEVEL <= 8 THEN 4
ELSE 5
END FROM DUAL
CONNECT BY LEVEL <= 9;
<强>查询强>:
SELECT XMLELEMENT(
"Root",
XMLAGG(
XMLELEMENT(
"Parent",
XMLATTRIBUTES(
p.id,
p.attr1 AS "A",
p.attr2 AS "B"
),
c.child_xml
)
)
).getClobVal() AS xml
FROM parents p
LEFT OUTER JOIN (
SELECT parent_id,
XMLAGG(
XMLELEMENT(
"Child",
XMLATTRIBUTES( id ),
attr1
)
) AS child_xml
FROM children
GROUP BY parent_id
) c
ON ( p.id = c.parent_id );
<强>输出强>:
XML
-------------------------------------------------------------------------------------------
<Root><Parent ID="1" A="A.1" B="B.1"><Child ID="1">C.1</Child><Child ID="2">C.2</Child>
<Child ID="3">C.3</Child></Parent><Parent ID="2" A="A.2" B="B.2"><Child ID="4">C.4</Child>
<Child ID="5">C.5</Child><Child ID="6">C.6</Child></Parent><Parent ID="3" A="A.3" B="B.3">
</Parent><Parent ID="4" A="A.4" B="B.4"><Child ID="7">C.7</Child><Child ID="8">C.8</Child>
</Parent><Parent ID="5" A="A.5" B="B.5"><Child ID="9">C.9</Child></Parent></Root>
如果你想在PL / SQL中使用它,那么就这样做:
DECLARE
p_xml CLOB;
BEGIN
SELECT ...
INTO p_xml
FROM ...;
-- Then use the xml value.
END;
/