免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 12540 | 回复: 16
打印 上一主题 下一主题

xml文件的中文问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-17 20:31 |只看该作者 |倒序浏览
PERL的xml:arser 有支持gbk或gb2312 charset 的吗?
缺省只有big5。

读包含中文的xml文件,我这样写
     
<?xml version="1.0" encoding="big5"?>;

<ETLConfig>;
    <database S="test" U="测试" P="测试"/>;
    <database S="aaa" U="aaa" P="aaa"/>;
</ETLConfig>;

输出的是乱码。
use XML::Simple;
use Data:umper;

my $config = XMLin('c:/server.xml');          # load the file

print Dumper($config);
use bytes;

print  $config->;{database}[0]->;{U};                             
no bytes;

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-06-18 08:41 |只看该作者

xml文件的中文问题

應該跟encode沒關係..我沒用過XML::Simple
但是XML::Simple是外國人寫的...所以不可能
default用big5的...
Default應該是utf8...或其它的...
試試看把下面這行改成
<?xml version="1.0" encoding="gb2312"?>;
看看...
Perl有本Perl and XML的書..我覺得字太小一直沒看...
看了兩頁就放棄了...
去找找有沒有電子書翻翻吧..

论坛徽章:
0
3 [报告]
发表于 2003-06-18 15:20 |只看该作者

xml文件的中文问题

沿着前人的路走了一遍,得出的结果和他的一样。难道这条路没人走通过吗?

The XML:arser installed from CPAN does not come with a
GB2312 encoding support. However, I was not able to add
the support as instructed by the XML::Encoding package.

To add this support, I did the following:

1. Download GB2312.TXT from ftp.unicode.org
2. Download the XML::Encoding 1.01 and get two binaries:
   make_encmap and compile_encoding
3. run make_encmap as follows:
   make_encmap GB2312 GB2312.TXT >; GB2312.encmap
4. Add expat='yes' to the first line of GB2312.encmap
5. run compile_encoding:
   compile_encoding -o GB2312.enc GB2312.encmap
6. copy GB2312.enc to
   /usr/lib/perl5/site_perl/5.005/i386-linux/XML/Parser/Encodings

Then I made the following perl script:
---------------
#!/usr/bin/perl
use XML:arser;

my $xmlfile = $ARGV[0];
my $parser = new XML:arser();
my $doc = $parser->;parsefile ("$xmlfile";
---------------

I run this script with a well-formed xml file having a head line
as: <?xml version="1.0" encoding="GB2312"?>;

Following error occurs:

unknown encoding at line 1, column 30, byte 30 at /usr/lib/perl5/site_perl/5.005/i386-linux/XML/Parser.pm line 185

Changing the encoding to other supported ones seem to work without error.
I'm wondering if there is something I'm missing in the process.

Thanks for any suggestions!

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-06-19 08:45 |只看该作者

xml文件的中文问题

會不會是大小寫的問題...改成
gb2312.enc
我剛剛檢查過
/usr/local/lib/perl5/site_perl/5.6.1/aix/XML/Parser/Encodings
這個目錄下:
-r--r--r--   1 root     system      4821 Feb 14 2000  Japanese_Encodings.msg
-r--r--r--   1 root     system      1946 Feb 14 2000  README
-r--r--r--   1 root     system     40706 May 10 2000  big5.enc
-r--r--r--   1 root     system     45802 May 10 2000  euc-kr.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-2.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-3.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-4.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-5.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-7.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-8.enc
-r--r--r--   1 root     system      1072 May 10 2000  iso-8859-9.enc
-r--r--r--   1 root     system      1072 May 10 2000  windows-1250.enc
-r--r--r--   1 root     system     37890 May 10 2000  x-euc-jp-jisx0221.enc
-r--r--r--   1 root     system     37890 May 10 2000  x-euc-jp-unicode.enc
-r--r--r--   1 root     system     20368 May 10 2000  x-sjis-cp932.enc
-r--r--r--   1 root     system     18202 May 10 2000  x-sjis-jdk117.enc
-r--r--r--   1 root     system     18202 May 10 2000  x-sjis-jisx0221.enc
-r--r--r--   1 root     system     18202 May 10 2000  x-sjis-unicode.enc

都是小寫...
另外..你可以在.pl前頭加上...
use Carp();
local $SIG{__WARN__} = \&Carp::cluck;

Trace一下..看到底是哪裡一行出問題..
185行是parse,他後面還有call其他function..
不然..實在看不出來哪兒出錯了...

论坛徽章:
0
5 [报告]
发表于 2003-06-19 18:00 |只看该作者

xml文件的中文问题

大写改成小写了,但还是同样的错。
gb2312.TXT见http://www.unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/GB/
能不能帮忙看一下。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-06-19 19:12 |只看该作者

xml文件的中文问题

http://aspn.activestate.com/ASPN/Mail/Message/perl-xml/937990
http://lists.xml.org/archives/xml-dev/200208/msg01661.html
http://www.xml.com/pub/a/1999/09/expat/index.html

看看吧...
看起來是因為expat不支援gb2312..
所以你可能要先轉碼成utf8..
或者用他上面所提的另一個Parser
或者..改寫XML:arser..讓他支援gb2312...

论坛徽章:
0
7 [报告]
发表于 2003-06-20 22:26 |只看该作者

xml文件的中文问题

expat的最新版本有很大的变化,不知道它怎样被perl调用的.

www.sourceforge.net/projects/expat

Expat is a library, written in C, for parsing XML documents. It's the underlying XML parser for the open source Mozilla project, Perl's XML:arser, Python's xml.parsers.expat, and other open-source XML parsers.

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2003-06-22 11:37 |只看该作者

xml文件的中文问题

我試著去Trace XML:arser這個module...
不過看到蠻多看不懂的東西..
他裡面有用到
XML:arser::Expat這個module..
在XML:arser::Expat這個module中有個
load_encoding的function,是當Expat module
找不到相關 encoding時會自動呼叫,
我試著依照document的方式自己Load
gb2312.enc,[其中LoadEncoding的結果,可以得到正確的
GB2312,但是在相對應的%Encoding_Table中卻無法得到
這個gb2312.enc的編碼,因此才無法支援gb2312的編碼


  1. local(*ENC);
  2. open(ENC, $file) or croak("Couldn't open encmap $file:\n$!\n");
  3. binmode(ENC);
  4. my $data;
  5. my $br = sysread(ENC, $data, -s $file);
  6. croak("Trouble reading $file:\n$!\n")
  7.    unless defined($br);
  8. close(ENC);

  9. my $name = LoadEncoding($data, $br);
  10. #print "$name\n";
  11. croak("$file isn't an encmap file")
  12.    unless defined($name);
复制代码


現在問題縮小到LoadEncoding($data,$br);
這行,可以正確的讀出GB2312編碼,但是卻無法存入
%Encoding_Table中,
這個LoadEncoding function 我還沒找到在哪裡...
有興趣的人可以follow下去...

论坛徽章:
0
9 [报告]
发表于 2003-06-22 11:59 |只看该作者

xml文件的中文问题

LoadEncoding函数是C代码。
在XML-Parser-2.31的Expat.c中。

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
10 [报告]
发表于 2003-06-23 08:41 |只看该作者

xml文件的中文问题

應該是Expat/Expat.xs裡面...XML_LoadEncoding function...
我C的功力還沒那麼好...所以可能得找C的高手幫忙看看..
因為他會產生一個Expat.so的share object在auto/XML/Parser/Expat目錄下,
可以直接蓋掉上面目錄中的那個...或者重新安裝XML:arser...
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP