Oracle SQL XML Functions - How to get encoding in XML Prolog?

╄→гoц情女王★ 提交于 2019-12-10 18:20:20

问题


This SQL

SELECT XMLRoot(XMLType('<poid>143598</poid>'), VERSION '1.0', STANDALONE YES)
  AS xmlroot FROM DUAL; 

generates an output as follows

XMLROOT
--------------------------------------
<?xml version="1.0" standalone="yes"?>
<poid>143598</poid>

How can get encoding in my xml prolog?

Ex - I want output to be something like

XMLROOT
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<poid>143598</poid>

Reference -

Generate XML Data from the database


回答1:


select xmlroot (xmltype ('<poid>143598</poid>')
                  , version '1.0" encoding="UTF-8'
                  ) "XMLRoot"
  from dual;



回答2:


Weird ... but looks like version argument can have anything in it -

replace

version '1.0'

with

version '1.0" encoding="utf-8'

output

XMLROOT
--------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<poid>143598</poid>



回答3:


It is easy but let me explain the possible logic of Oracle:

Character data are stored in database encoding by default. If you want to specify encoding then probable it is different from database encoding. OK, Let it be BLOB, i.e. octet stream represented in desired encoding. So we should use XMLSERIALIZE function to create representation in any encoding (including the default DB encoding as well)

select  
  xmlserialize(document xmltype('<Envelop>Any UTF charachers. Tous les caractères UTF. כל תווי UTF </Envelop>') as blob encoding 'UTF-8' version '1.0')
from dual;

If your default DB encoding is UTF-8 then you can also wrap this call into to_clob(…) or even to_char(…) to see the result. For me

select  
  to_char(xmlserialize(document xmltype('<Envelop>Any UTF charachers. Tous lescaractères UTF. כל תווי UTF </Envelop>') as blob encoding 'UTF-8' version '1.0'))
from dual;

Gives:

<?xml version="1.0" encoding="UTF-8"?>
<Envelop>Any UTF charachers. Tous les caractères UTF. כל תווי UTF </Envelop>


来源:https://stackoverflow.com/questions/12784478/oracle-sql-xml-functions-how-to-get-encoding-in-xml-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!