I just got started using XMLType. I know maybe a little late to start to look at it, but better late than never, right?
Anyways, to see what is going on with an XMLType variable in my PL/SQL code, I wanted to use DBMS_OUTPUT to display the
information in SQL*Plus. On this project, we are running Oracle 9.2.0.5 and DBMS_OUTPUT does not play well with XMLType. On my laptop, I am running
Oracle 10g Release 1, XMLType and DBMS_OUTPUT do not mix either.
Even Google could not shed any light how to combine XMLType and DBMS_OUTPUT. What I wanted was to show the whole XML document and not just a
few bits and pieces. Am I the only one who wants to show XMLType using DBMS_OUTPUT? Probably my Google-skills are not that great.
Long story short
One of my colleagues wrote a little procedure to show XMLType using DBMS_OUTPUT:
procedure show_xml (p_xml in xmltype
,p_line_length in number default 100
)
is
l_loops number;
l_mod number;
l_xml clob;
begin
l_xml := p_xml.getClobVal();
l_loops := floor(length(l_xml)/50);
l_mod := mod(length(l_xml),50);
for l_index in 1..l_loops
loop
dbms_output.put_line (substr (l_xml,1 + ((l_index - 1) * p_line_length),p_line_length));
end loop;
dbms_output.put_line (substr (l_xml,1 + ((l_loops) * p_line_length),l_mod));
end show_xml;
This little procedure transforms the XMLType parameter to a CLOB, and the loop breaks it up in smaller
bite size pieces for DBMS_OUTPUT to handle.
The disadvantage of this technique is that the way the XML is shown, namely like this:
SQL> declare
2 x xmltype;
3 begin
4 x := xmltype ('<?xml version="1.0" encoding="utf-8"?><Employees><Employee>
5 <No>7369</No><Name>Smith</Name><Job>Clerk</Job></Employee><Employee>
6 <No>7499</No><Name>Allen</Name><Job>Salesman</Job></Employee><Employee>
7 <No>7521</No><Name>Ward</Name><Job>Salesman</Job></Employee></Employees>');
8 show_xml(x);
9 end;
10 /
<?xml version="1.0" encoding="utf-8"?><Employees><Employee>
<No>7369</No><Name>Smith</Name><Job>C
lerk</Job></Employee><Employee>
<No>7499</No><Name>Allen</Name><Job>Salesman</Job></Employee><Emp
loyee>
<No>7521</No><Name>Ward</Name><Job>Salesman</Job></Employee></Employees>
PL/SQL procedure successfully completed.
SQL>
The XML that is shown is not really nicely formatted, making it hard to read.
After some fiddling ’round and a little bit of luck, I came up with this:
procedure show_xml (p_xml in xmltype)
is
l_str long;
begin
l_str := p_xml.extract('/*').getstringval();
loop
exit when l_str is null;
dbms_output.put_line (substr (l_str, 1, instr (l_str, chr(10)) - 1));
l_str := substr (l_str, instr (l_str, chr(10)) + 1);
end loop;
end show_xml;
This uses a XPath expression to retrieve the whole XML-instance into a local variable. The argument of the extract
method is ‘/*’. The slash identifies the root-node and the star is a wildcard.
Next it breaks this local variable up into smaller pieces, based on the position of
the end-of-line characters.
And here is the result:
SQL> declare
2 x xmltype;
3 begin
4 x := xmltype ('<?xml version="1.0" encoding="utf-8"?><Employees><Employee>
5 <No>7369</No><Name>Smith</Name><Job>Clerk</Job></Employee><Employee>
6 <No>7499</No><Name>Allen</Name><Job>Salesman</Job></Employee><Employee>
7 <No>7521</No><Name>Ward</Name><Job>Salesman</Job></Employee></Employees>');
8 show_xml(x);
9 end;
10 /
<Employees>
<Employee>
<No>7369</No>
<Name>Smith</Name>
<Job>Clerk</Job>
</Employee>
<Employee>
<No>7499</No>
<Name>Allen</Name>
<Job>Salesman</Job>
</Employee>
<Employee>
<No>7521</No>
<Name>Ward</Name>
<Job>Salesman</Job>
</Employee>
</Employees>
PL/SQL procedure successfully completed.
SQL>
If you have a Oracle 10g R2 database, it is not even necessary break the string (l_str in the procedure) up into smaller pieces. The limit
of 255 characters has finally been removed!
To save you from a copy-paste action for this blog, here is the source code for the procedure. www.w3schools.com has a great tutorial on the use of XPath expressions.
Thanks for this, I was racking my head how to pretty-print some XML out of an XMLTYPE (it seems to lose all formatting as soon as you do an XMLTYPE function on it – e.g. UPDATEXML etc) and now I can just use the EXTRACT(‘/*’) ! Marvellous!
See the XMLDB forum: http://forums.oracle.com/forums/search.jspa?objID=f34&q=ora-31011 for ideas
RE: executing
select XMLElement(â€TheDateâ€, sysdate) from dual
> ok if your database is not UTF-8
> errors with ‘ora-31011 xml parsing failed’ if database is UTF-8
I have yet to find out what’s causing this is and what the solution/workaround is…
Hi Han,
I think this has to do with the SQL*Plus client that you use. At this site, I use SQL*Plus: Release 9.2.0.6.0 and it yields this result:
SQL> select XMLElement(“TheDate”, sysdate) from dual;
XMLELEMENT(“THEDATE”,SYSDATE)
—————————————————–
18-SEP-06
I do remember that we used to have an older version and it would show the XMLTypes as you describe.
I can’t use this statement, do i need to install extra XML stuff on the database ?
SQLPlus Release 9.0.1.4.0
SQL> select XMLElement(â€TheDateâ€, sysdate) from dual;
XMLELEMENT(â€THEDATEâ€,SYSDATE)()
——————————————————————————–
XMLTYPE()
Thanks Marco for this link to the Forums! Like I said my Google skills aren’t that great. I would never thought of using “pretty print” as a search term.
Changing the Long to a LOB? Probably. Maybe it would suffice to use VARCHAR2… I can’t get used to DBMS_LOB to manipulate Lobs…
For “extract” and “pretty print” see also http://forums.oracle.com/forums/search.jspa?objID=f34&q=extract+pretty+print
Alex,
wouldn’t it be better to use “clob” instead of your “long” datatype (l_str=long; – anyway will the long data type not be depricated, despite the fact that is a nasty datatype to handle)?.
The pretty print is caused because you used “extract(’/*’)” this (=extract) will give the layout you want, or not want, pretty print layout is the default behaviour for “extract” (you can alter this behaviour if you are using XML schemas and using a high Oracle 10.2.x version).