gpt4 book ai didi

sql - 生成 XML 文件时收到 PL/SQL 错误

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:03 26 4
gpt4 key购买 nike

我正在尝试使用以下代码使用 PL/SQL 过程制作 XML 文件:-

create or replace directory temp_dir as 'C:\XML';
grant read, write on directory temp_dir to hr;

DECLARE
doc DBMS_XMLDOM.DOMDocument;
xdata XMLTYPE;

CURSOR xmlcur IS
SELECT xmlelement("Employee",XMLAttributes('http://www.w3.org/2001/XMLSchema' AS "xmlns:xsi",
'http://www.oracle.com/Employee.xsd' AS "xsi:nonamespaceSchemaLocation")
,xmlelement("EmployeeNumber",e.employee_id)
,xmlelement("EmployeeName",e.first_name)
,xmlelement("Department",xmlelement("DepartmentName",d.department_name)
,xmlelement("Location",d.location_id)
)
)
FROM employees e
,departments d
WHERE e.department_id=d.department_id;

BEGIN
OPEN xmlcur;
FETCH xmlcur INTO xdata;
CLOSE xmlcur;
doc := DBMS_XMLDOM.NewDOMDocument(xdata);
DBMS_XMLDOM.WRITETOFILE(doc, 'temp_dir/myXML1.xml');
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20002,'Error writing out XML.');
END;

但我收到以下错误:-

Line 185: ORA-29280: invalid directory path
ORA-06512: at "SYS.UTL_FILE", line 41
ORA-06512: at "SYS.UTL_FILE", line 478
ORA-06512: at "XDB.DBMS_XSLPROCESSOR", line 217
ORA-29280: invalid directory path
ORA-29280: invalid directory path
ORA-06512: at "XDB.DBMS_XMLDOM", line 5292
ORA-06512: at line 23

我做错了什么。

最佳答案

首先,您正在尝试写入数据库服务器上的文件,对吗?不是您客户端计算机上的文件? c:\xml 似乎是一个目录,可能存在于您的客户端机器上,而不是服务器上。

DBMS_XMLDOM documentation

Note: Before database startup, the read-from and write-to directories in the init.ORA file must be specified; for example: UTL_FILE_DIR=/mypath/insidemypath.

Read-from and write-to files must be on the server file system.

DBMS_XMLDOM 似乎不支持目录对象。它似乎仍然依赖于旧的 UTL_FILE_DIR 参数。

就个人而言,我可能会使用 getClobVal() 方法将 XMLType 转换为 CLOB,然后使用 DBMS_XSLPROCESSOR 中的 clob2file 过程打包写出来

DBMS_XSLProcessor.Clob2File( 
xdata.getClobVal(),
'TEMP_DIR',
'myXML1.xml' );

在我的系统上运行

SQL> ed
Wrote file afiedt.buf

1 DECLARE
2 xdata XMLTYPE;
3 CURSOR xmlcur IS
4 SELECT xmlelement("Employee",
5 XMLAttributes('http://www.w3.org/2001/XMLSchema' AS "xmlns:xsi",
6 'http://www.oracle.com/Employee.xsd' AS "xsi:nonamespaceSchemaLocation")
7 ,xmlelement("EmployeeNumber",e.employee_id)
8 ,xmlelement("EmployeeName",e.first_name)
9 ,xmlelement("Department",
10 xmlelement("DepartmentName",d.department_name)
11 ,xmlelement("Location",d.location_id)
12 )
13 )
14 FROM employees e
15 ,departments d
16 WHERE e.department_id=d.department_id;
17 BEGIN
18 OPEN xmlcur;
19 FETCH xmlcur INTO xdata;
20 CLOSE xmlcur;
21 DBMS_XSLProcessor.Clob2File(
22 xdata.getClobVal(),
23 'TEMP_DIR',
24 'myXML1.xml' );
25* END;
SQL> /

PL/SQL procedure successfully completed.

如果你真的想从游标中获取所有数据而不仅仅是第一行,你将需要一个循环

SQL> ed
Wrote file afiedt.buf

1 DECLARE
2 xdata XMLTYPE;
3 CURSOR xmlcur IS
4 SELECT xmlelement("Employee",
5 XMLAttributes('http://www.w3.org/2001/XMLSchema' AS "xmlns:xsi",
6 'http://www.oracle.com/Employee.xsd' AS "xsi:nonamespaceSchemaLocation")
7 ,xmlelement("EmployeeNumber",e.employee_id)
8 ,xmlelement("EmployeeName",e.first_name)
9 ,xmlelement("Department",
10 xmlelement("DepartmentName",d.department_name)
11 ,xmlelement("Location",d.location_id)
12 )
13 )
14 FROM employees e
15 ,departments d
16 WHERE e.department_id=d.department_id;
17 l_clob clob;
18 BEGIN
19 dbms_lob.createtemporary( l_clob, true );
20 OPEN xmlcur;
21 LOOP
22 FETCH xmlcur INTO xdata;
23 EXIT WHEN xmlcur%notfound;
24 l_clob := l_clob || xdata.getClobVal();
25 END LOOP;
26 DBMS_XSLProcessor.Clob2File(
27 l_clob,
28 'TEMP_DIR',
29 'myXML1.xml' );
30 CLOSE xmlcur;
31* END;
SQL> /

PL/SQL procedure successfully completed.

关于sql - 生成 XML 文件时收到 PL/SQL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799269/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com