2008年8月13日星期三

Re: [PerlChina] XML中文


open(my $fh, '<', $file);
local $/ = undef;
my $string = <$fh>;
close($fh);
use Encode qw/from_to/;
from_to($string, 'gb2312', 'utf8');
use XML::Simple;
my $xs1 = new XML::Simple;
my $doc = $xs1->XMLin($string);

试试。

use Encode qw/from_to/;
from_to($string, 'gb2312', 'utf8');

试了一下XML::Parser也可以了,多谢。
对于本身就是UTF8的再from_to一次会有问题,怎么判断$string是否utf8?直接让它parse一遍,不行的话再encode?

另外,我在中文字符外面套了一层<![CDATA[     ]]>,按说DOM/Parser应该不会去管里面是什么内容?
 
--
>: ~

Re: [PerlChina] XML中文

Question wrote:
> 2008/8/13 Fayland Lam <fayland@gmail.com <mailto:fayland@gmail.com>>
>
>
> binmode STDOUT 是不中的,要去把原始的 $file 转为 utf8
> 否则得用 Encode 来转为 utf8
>
>
> XML::Encoding / Unicode::String?
> perl做web的这么多,能不能给个例子?
>
> 也就是说自己读数据再encoding utf8,不用parseFile 用 parse/parsestring?
>

open(my $fh, '<', $file);
local $/ = undef;
my $string = <$fh>;
close($fh);
use Encode qw/from_to/;
from_to($string, 'gb2312', 'utf8');
use XML::Simple;
my $xs1 = new XML::Simple;
my $doc = $xs1->XMLin($string);

试试。

> --
> >: ~
> ------------------------------------------------------------------------
>
> _______________________________________________
> China-pm mailing list
> China-pm@pm.org
> http://mail.pm.org/mailman/listinfo/china-pm


--
Fayland Lam // http://www.fayland.org/
Foorum based on Catalyst // http://www.foorumbbs.com/

_______________________________________________
China-pm mailing list
China-pm@pm.org
http://mail.pm.org/mailman/listinfo/china-pm

Re: [PerlChina] XML中文

2008/8/13 Fayland Lam <fayland@gmail.com>

binmode STDOUT 是不中的,要去把原始的 $file 转为 utf8
否则得用 Encode 来转为 utf8

XML::Encoding /  Unicode::String?
perl做web的这么多,能不能给个例子?

也就是说自己读数据再encoding utf8,不用parseFile 用 parse/parsestring?

--
>: ~

2008年8月12日星期二

Re: [PerlChina] XML中文

Question wrote:
> 各位不吝赐教啊,难道真要自己搞regexp?
> DOM不支持中文。。。这个匪夷所思啊。
>

binmode STDOUT 是不中的,要去把原始的 $file 转为 utf8
否则得用 Encode 来转为 utf8

> 2008/8/13 Question <wanliyou@gmail.com <mailto:wanliyou@gmail.com>>
>
> 现在用这个
> use XML::Simple;
> my $xs1 = new XML::Simple;
>
> my $doc = $xs1->XMLin($file);
>
> 读入是正常的,但中文print出来就不对了,binmode(STDOUT, ":utf8")
> 更不行。。。
>
>
> --
> >: ~
>
>
>
>
> --
> >: ~
> ------------------------------------------------------------------------
>
> _______________________________________________
> China-pm mailing list
> China-pm@pm.org
> http://mail.pm.org/mailman/listinfo/china-pm


--
Fayland Lam // http://www.fayland.org/
Foorum based on Catalyst // http://www.foorumbbs.com/

_______________________________________________
China-pm mailing list
China-pm@pm.org
http://mail.pm.org/mailman/listinfo/china-pm

Re: [PerlChina] XML中文

各位不吝赐教啊,难道真要自己搞regexp?
DOM不支持中文。。。这个匪夷所思啊。

2008/8/13 Question <wanliyou@gmail.com>
现在用这个
    use XML::Simple;
    my $xs1 = new XML::Simple;

    my $doc = $xs1->XMLin($file);

读入是正常的,但中文print出来就不对了,binmode(STDOUT, ":utf8")更不行。。。


--
>: ~



--
>: ~

Re: [PerlChina] XML中文

现在用这个
    use XML::Simple;
    my $xs1 = new XML::Simple;

    my $doc = $xs1->XMLin($file);

读入是正常的,但中文print出来就不对了,binmode(STDOUT, ":utf8")更不行。。。


--
>: ~

Re: [PerlChina] XML中文

2008/8/13 Fayland Lam <fayland@gmail.com>
Question wrote:
> Hi,
> 现在想用perl解析一个asx(xml)文件,现在用XML::DOM发现不支持中文。

http://wiki.perlchina.org/index.php/Perl-XML-FAQ
http://wiki.perlchina.org/index.php/%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8BPerl_XML%EF%BC%9A%E6%8E%A5%E5%8F%A3%E7%AF%87

一般来说,最好你去解析 utf8 的 xml 文件
如果需要解析 gb2312 的文件的话,下载
http://www.fayland.org/misc/backup/gb2312.enc
找到 XML::Parser 的地址

bash-3.00$ perldoc -l XML::Parser
/usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi/XML/Parser.pm

然后下载过去放到
/usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi/XML/Parser/Encodings/
下试试。

Thanks.


谢谢各位,文件是这样的,asx文件
<asx version = "3.0">
    <title>CCTV视频</title>
    <abstract></abstract>
    <copyright>XMLFDFFD</copyright>
    <author>FREE</author>
    <entry>
        <title>CCTV视频</title>
        <abstract></abstract>
        <copyright>XMLFDFFD</copyright>
        <author>FREE</author>
        <STARTTIME VALUE="00:10:00" />
        <DURATION VALUE="00:20:00" />
        <ref href = "mms://201.174.195.1/abc.wmv"/>
    </entry>
</asx>

我自己在前面加上<?xml version="1.0" encoding="UTF-8"?>
CCTV视频 加上cdata也是不行。

    use XML::Parser;
    my $parser = new XML::Parser();
    $parser->parsefile("test\\test.asx");


--
>: ~

Re: [PerlChina] XML中文

Question wrote:
> Hi,
> 现在想用perl解析一个asx(xml)文件,现在用XML::DOM发现不支持中文。

http://wiki.perlchina.org/index.php/Perl-XML-FAQ
http://wiki.perlchina.org/index.php/%E5%BF%AB%E9%80%9F%E5%BC%80%E5%A7%8BPerl_XML%EF%BC%9A%E6%8E%A5%E5%8F%A3%E7%AF%87

一般来说,最好你去解析 utf8 的 xml 文件
如果需要解析 gb2312 的文件的话,下载
http://www.fayland.org/misc/backup/gb2312.enc
找到 XML::Parser 的地址

bash-3.00$ perldoc -l XML::Parser
/usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi/XML/Parser.pm

然后下载过去放到
/usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi/XML/Parser/Encodings/
下试试。

Thanks.

> 文件大概是这样的:
>
> <asx version = "3.0">
> <title>视频</title>
> <abstract></abstract>
> </asx>
>
> google了一圈没什么结果,自己用regex匹配太累。
> 各位有什么办法么?
>
>
> --
> >: ~
> ------------------------------------------------------------------------
>
> _______________________________________________
> China-pm mailing list
> China-pm@pm.org
> http://mail.pm.org/mailman/listinfo/china-pm


--
Fayland Lam // http://www.fayland.org/
Foorum based on Catalyst // http://www.foorumbbs.com/

_______________________________________________
China-pm mailing list
China-pm@pm.org
http://mail.pm.org/mailman/listinfo/china-pm

[PerlChina] 答复: XML中文

用XML::Parser

________________________________________
发件人: china-pm-bounces+beckheng=sina.com@pm.org [mailto:china-pm-bounces+beckheng=sina.com@pm.org] 代表 Question
发送时间: 2008年8月13日 10:35
收件人: china-pm
主题: [PerlChina] XML中文

Hi,
现在想用perl解析一个asx(xml)文件,现在用XML::DOM发现不支持中文。
文件大概是这样的:

<asx version = "3.0">
<title>视频</title>
<abstract></abstract>
</asx>

google了一圈没什么结果,自己用regex匹配太累。
各位有什么办法么?


--
>: ~
_______________________________________________
China-pm mailing list
China-pm@pm.org
http://mail.pm.org/mailman/listinfo/china-pm

[PerlChina] XML中文

Hi,
   现在想用perl解析一个asx(xml)文件,现在用XML::DOM发现不支持中文。
   文件大概是这样的:

<asx version = "3.0">
        <title>视频</title>
        <abstract></abstract>
</asx>

google了一圈没什么结果,自己用regex匹配太累。
各位有什么办法么?


--
>: ~