有朋友问到关于XMLDOM无法正确设置字符集的问题,也就是xmldom.setCharset无效的问题。 查询一下Metalink,参考Note:251
有朋友问到关于XMLDOM无法正确设置字符集的问题,也就是xmldom.setCharset无效的问题。
查询一下Metalink,参考Note:251011.1,这是Oracle的一个Bug,,可以通过dbms_output来绕过这个问题。
在Oracle Databsae 10g中测试:
SQL> select * from v$version
2 /
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.2.0 - Prod
PL/SQL Release 10.2.0.2.0 - Production
CORE 10.2.0.2.0 Production
TNS for Linux: Version 10.2.0.2.0 - Production
NLSRTL Version 10.2.0.2.0 - Production
效果如下,这是一个普遍性问题,在9i、10g中都存在:
SQL> create or replace procedure test_SETCHARSET
2 is
3 doc xmldom.DOMDocument;
4 main_node xmldom.DOMNode;
5 root_node xmldom.DOMNode;
6
7 item_node xmldom.DOMNode;
8 root_elmt xmldom.DOMElement;
9 item_elmt xmldom.DOMElement;
10 item_text xmldom.DOMText;
11
12
13
14 buffer_problem varchar2(2000);
15 buffer_root_node varchar2(2000);
16 buffer_doc_header varchar2(80);
17 buffer_doc varchar2(2000);
18 reqRootNode xmldom.DOMNode;
19
20 BEGIN
21 --
22 -- the problem :
23 --
24 dbms_output.put_line('=========== ');
25 dbms_output.put_line(' PROBLEM: setCharSet ISO-8859-1 has no effect' );
26
27 doc := xmldom.newDOMDocument;
28 main_node := xmldom.makeNode(doc);
29 xmldom.setversion(doc,'1.0');
30 xmldom.setCharset(doc,'ISO-8859-1');
31 root_elmt := xmldom.createElement(doc, 'A');
32 root_node := xmldom.appendChild( main_node, xmldom.makeNode(root_elmt));
33
34 item_elmt := xmldom.createElement(doc, 'B');
35 item_node := xmldom.appendChild(root_node, xmldom.makeNode(item_elmt));
36 -- chr (192) : LATIN CAPITAL LETTER A WITH GRAVE in ISO-8859-1
37 item_text := xmldom.createTextNode(doc, 'X' ||chr (192) ||'X');
38 item_node := xmldom.appendChild(item_node, xmldom.makeNode(item_text));
39
40 xmldom.writetobuffer(doc, buffer_problem);
41 -- the final document here is encoded in UTF8
42 dbms_output.put_line(buffer_problem);
43 dbms_output.put_line(' ');
44 dbms_output.put_line('=========== ');
45
46 --
47 -- workaround
48 --
49
50 dbms_output.put_line(' WORKAROUND:' );
51 dbms_output.put_line(' ');
52 buffer_doc_header := '';
53 reqRootNode := xmldom.makeNode (xmldom.getDocumentElement(doc));
54
55 xmldom.writetobuffer(reqRootNode, buffer_root_node);
56 buffer_root_node := convert (buffer_root_node,'WE8ISO8859P1','UTF8');
57 buffer_doc := buffer_doc_header || buffer_root_node;
58
59 dbms_output.put_line(buffer_doc );
60 dbms_output.put_line('=========== ');
61 --
62
63 xmldom.freeDocument(doc);
64
65 END;
66 /
Procedure created.
SQL> set serveroutput on
SQL> exec test_setcharset
===========
PROBLEM: setCharSet ISO-8859-1 has no effect
XX
===========
WORKAROUND:
XX
===========
PL/SQL procedure successfully completed.