从 PL/SQL 开发人员处读取 XML 文件
Posted
技术标签:
【中文标题】从 PL/SQL 开发人员处读取 XML 文件【英文标题】:Reading XML file from PL/SQL developer 【发布时间】:2011-05-02 06:44:47 【问题描述】:以下是我从网络中获取的过程..但是在执行它时给我错误“必须声明 xmlgen.setRowsetTag”
请告诉我这个错误是由于一些设置错误还是我必须包含一些文件.. 我正在使用 PL/SQL developer 10g
谢谢:-
create or replace procedure SP_XML_TEST is
begin
declare
xmlString CLOB := null;
-- Here we are reading 250 bytes at a time. We should be really reading a
-- whole chunk. dbms_output.put_line can only accomodate 256 characters per line
-- so we have this limitation.
amount integer:= 255;
position integer := 1;
charString varchar2(255);
begin
xmlgen.setRowTag('EMP_ROW'); -- we want the row element to be named EMP_ROW.
xmlgen.setRowsetTag('EMP_RESULTS'); -- we want the result document root to be EMP_RESULTS.
xmlgen.setMaxRows(3); -- limit the output to 3 rows.
xmlgen.setskipRows(2); -- skip the first two rows in the query before outputing results.
xmlgen.useLowerCaseTagNames(); -- set the tag names to be all in lower case.
xmlgen.setErrorTag('ERROR_RESULT'); -- set the ERROR tag to be ERROR_RESULTS.
xmlgen.setRowIdAttrName('ENO'); -- set the id attribute in the ROW element to be ENO.
xmlgen.setRowIdColumn('EMPNO'); -- use the EMPNO column's value for the id attribute.
xmlgen.useNullAttributeIndicator(false); -- do not use the null indicator to indicate nullness.
xmlgen.setStyleSheet('http://www.oracle.com/xsl'); -- attach the stylesheet PI to the result document.
xmlString := xmlgen.getXML('select * from scott.emp ',1); -- This gets the XML out
dbms_lob.open(xmlString,DBMS_LOB.LOB_READONLY); -- Now open the lob data..
loop
dbms_lob.read(xmlString,amount,position,charString); -- read the lob data
dbms_output.put_line(charString);
position := position + amount;
end loop;
exception
when no_data_found then
dbms_lob.close(xmlString); -- end of fetch, free the lob
dbms_lob.freetemporary(xmlString);
xmlgen.resetOptions;
when others then
xmlgen.resetOptions;
end;
end SP_XML_TEST;
【问题讨论】:
【参考方案1】:从 Das Interweb 取货的问题在于质量控制可能非常差。特别是您需要注意版本号。您使用的是 Oracle 10g。我认为 XMLGEN 是在 XDK for Oracle 8i 中引入的;在 9i 中它已被弃用,取而代之的是 PL/SQL 包 DBMS_XMLGEN,并且(据我所知)已经有几年没有包含在数据库中了。
DBMS_XMLGEN 与 XMLGEN 做类似的事情,但有些过程的签名略有不同,有些已被完全删除。我已经重写了发布的代码以使用 DBMS_XMLGEN。
create or replace procedure SP_XML_TEST is
xmlString CLOB;
amount integer:= 255;
position integer := 1;
charString varchar2(255);
len pls_integer;
l_ctx dbms_xmlgen.ctxHandle;
begin
l_ctx := dbms_xmlgen.newcontext('select * from apc.emp');
dbms_xmlgen.setRowTag(l_ctx, 'EMP_ROW'); -- we want the row element to be named EMP_ROW.
dbms_xmlgen.setRowsetTag(l_ctx, 'EMP_RESULTS'); -- we want the result document root to be EMP_RESULTS.
dbms_xmlgen.setMaxRows(l_ctx, 3); -- limit the output to 3 rows.
dbms_xmlgen.setskipRows(l_ctx, 2); -- skip the first two rows in the query before outputing results.
xmlString := dbms_xmlgen.getXML(l_ctx ); -- This gets the XML out
dbms_output.put_line('rows read = '||to_char(dbms_xmlgen.GETNUMROWSPROCESSED(l_ctx)));
len := dbms_lob.getlength (xmlString);
dbms_lob.open(xmlString,DBMS_LOB.LOB_READONLY); -- Now open the lob data..
loop
dbms_lob.read(xmlString,amount,position,charString); -- read the lob data
dbms_output.put_line(charString);
position := position + amount;
EXIT when position > len;
end loop;
dbms_lob.close(xmlString);
dbms_xmlgen.closecontext(l_ctx);
end SP_XML_TEST;
/
瞧!
SQL> exec SP_XML_TEST
rows read = 3
<?xml version="1.0"?>
<EMP_RESULTS>
<EMP_ROW>
<EMPNO>8085</EMPNO>
<ENAME>TRICHLER</ENAME>
<JOB>PLUMBER</JOB>
<MGR>8061</MGR>
<HIREDATE>08-APR-10</HIREDATE>
<SAL>3500</SAL>
<DEPTNO>50</DEPTNO>
</EMP_ROW>
<EMP_ROW>
<EMPNO>7369</EMPNO>
<
ENAME>CLARKE</ENAME>
<JOB>CLERK</JOB>
<MGR>7902</MGR>
<HIREDATE>17-DEC-80</HIREDATE>
<SAL>800</SAL>
<DEPTNO>20</DEPTNO>
</EMP_ROW>
<EMP_ROW>
<EMPNO>7499</EMPNO>
<ENAME>VAN WIJK</ENAME>
<JOB>SALESMAN</JOB>
<MGR>7698</MGR>
<HIREDATE>20
-FEB-81</HIREDATE>
<SAL>1600</SAL>
<COMM>300</COMM>
<DEPTNO>30</DEPTNO>
</EMP_ROW>
</EMP_RESULTS>
顺便说一句,自从原始编码器破解该过程以来发生的其他变化是对 DBMS_OUTPUT 缓冲区的限制。在 10g 中可以达到 32767。Find out more。
【讨论】:
以上是关于从 PL/SQL 开发人员处读取 XML 文件的主要内容,如果未能解决你的问题,请参考以下文章
是否可以在 PL/SQL 开发人员测试窗口的“声明”块中声明子过程?