oracle中xml的字符集问题

十月 13, 2009 – 3:04 下午

今天碰到个需求,解析ORACLE数据库中存储的xml文件。
这个xml的信息作为一个varchar2的字段存储在表中。
所以要使用xml的一些特性就首先需要将varchar2类型转换成xmltype。
在测试过程中又碰到了烦人的字符集问题。

举例如下:

sunwg@sunwg> create table sunwg(xml_1 xmltype);

Table created.

sunwg@sunwg> desc sunwg
Name Null? Type
————————– ——– ————————-
XML_1 XMLTYPE

创建了张有XMLTYPE字段的表sunwg。
现在向其中插入xml记录。


sunwg@sunwg> insert into sunwg values(
  2  xmltype('<?xml version="1.0" encoding="gb2312" ?>
  3  <FEATURELIST>
  4  <FEATURE id="62923" name="TOM" />
  5  <FEATURE id="62924" name="MARY" />
  6  </FEATURELIST>'));

1 row created.

sunwg@sunwg> commit;

Commit complete.

sunwg@sunwg> select * from sunwg;

XML_1
—————————————————————————————————-
<?xml version=”1.0″ encoding=”gb2312″ ?>
<FEATURELIST>
<FEATURE id=”62923″ name=”TOM” />
<FEATURE id=”62924″ name=”MARY” />
</FEATURELIST>

这个时候是没有问题的,那么我把name换成中文。

sunwg@sunwg> insert into sunwg values(
  2  xmltype(’<?xml version=”1.0″ encoding=”gb2312″ ?>
  3  <FEATURELIST>
  4  <FEATURE id=”62923″ name=”小猪” />
  5  <FEATURE id=”62924″ name=”小狗” />
  6  </FEATURELIST>’)); 
xmltype(’
*
ERROR at line 2:
ORA-31011: XML parsing failed
ORA-19202: Error occurred in XML processing
LPX-00216: invalid character 208 (0xD0)
Error at line 3
ORA-06512: at “SYS.XMLTYPE”, line 301
ORA-06512: at line 1

这次就出问题了,XML没有办法解析字符0xD0。

sunwg@sunwg> select dump(’小猪’,1016) from dual;

DUMP(’D??í’,1016)
———————————————–
Typ=96 Len=4 CharacterSet=US7ASCII: d0,a1,d6,ed

这个D0恰恰就是‘小’的第一个字节。
哎~~~

又是字符集的问题!!!

sunwg@sunwg> select * from v$nls_parameters where parameter =’ NLS_CHARACTERSET’;

PARAMETER VALUE
——————– —————————–
NLS_CHARACTERSET US7ASCII

我将下面的例子在另外一个GBK的数据库中运行就没有问题。
这个问题我也没想到有什么好的解决办法,暂时交给java那边处理了。
><

Post a Comment