Skip to Main Content
  • Questions
  • Trying to generate XML document with encoding specified

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Pete.

Asked: December 09, 2010 - 7:49 am UTC

Last updated: December 15, 2010 - 11:46 am UTC

Version: 11.2

Viewed 10K+ times! This question is

You Asked

Hi Tom,
We are trying to use XMLDOM / XMLType to create an XML document with the encoding set in the prolog e.g. <?xml version="1.0" encoding="utf-8"?>

we used the following PL/SQL block to create a document but the encoding does not come out: -

SQL> set serverout on size 999999
SQL> declare
  2    l_xml XMLDom.DomDocument;
  3    l_mainNode xmldom.DOMNode;
  4    l_xmltype xmltype;
  5  begin
  6         l_xml := xmldom.newDomDocument;
  7         l_mainNode := xmldom.makeNode( l_xml );
  8         dbms_xmldom.setVersion( l_xml, '1.0' );
  9         dbms_xmldom.setCharset( l_xml, 'UTF-8' );
 10         l_xmltype:= dbms_xmldom.getxmltype ( l_xml );
 11         dbms_output.put_line ( l_xmltype.getStringVal( ) );
 12  end;
 13  /
<?xml version="1.0"?>

PL/SQL procedure successfully completed.


and Tom said...

Sorry - I don't really do anything with XML myself, I'm not familiar enough with the API's to give you a quick answer.

Please try the XML forum on otn.oracle.com - you should be able to get a response very quickly over there.

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Appreciate the pointer

Pete, December 14, 2010 - 4:50 am UTC

Hi Tom,

I have raised this question on the XML forum as suggested.
Unfortunately, I have not currently had a response.

Would you be prepared to post this, so that perhaps someone who visits your site regularly, might have encountered this issue before?

Many thanks for responding.

Mike Kutz, December 14, 2010 - 1:50 pm UTC

http://forums.oracle.com/forums/thread.jspa?threadID=990936&tstart=405

line 8 is different

SQL> list
  1  declare
  2    l_xml XMLDom.DomDocument;
  3    l_mainNode xmldom.DOMNode;
  4    l_xmltype xmltype;
  5  begin
  6         l_xml := xmldom.newDomDocument;
  7         l_mainNode := xmldom.makeNode( l_xml );
  8         dbms_xmldom.setVersion( l_xml, '1.0" encoding="UTF-8' );
  9         dbms_xmldom.setCharset( l_xml, 'UTF-8' );
 10         l_xmltype:= dbms_xmldom.getxmltype ( l_xml );
 11         dbms_output.put_line ( l_xmltype.getStringVal( ) );
 12* end;
SQL> /
<?xml version="1.0" encoding="UTF-8"?>


PL/SQL procedure successfully completed.

Works fine

Pete, December 15, 2010 - 8:38 am UTC

Hi Mike,

Thanks for finding this.
Much appreciated.

Pete
Tom Kyte
December 15, 2010 - 11:46 am UTC

indeed, thanks!

dbms_xmldom.writeToFile(doc, xmlfile, 'UTF-8');

Felix, February 24, 2011 - 10:56 pm UTC

about the charset problem when creating xml file using dbms_xmldom, we can use
dbms_xmldom.writeToFile(doc, xmlfile, 'UTF-8');
to specify the xml encoding type.
And also, there is no need to wtire:
dbms_xmldom.setversion(doc, '1.0" encoding="UTF-8');

Hope this will help U~
BR,
Felix.Xu

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library