直接给个例子吧:
#!/usr/bin/env perl
# cnhackTNT
use strict;
use XML::DOM;
use Data::Dumper;
my $paser = XML::DOM::Parser->new;
my $doc = $paser->parse(<<XML);
<ScriptRule>
<Rule>
<BLineCode><![CDATA[JX-LINUX-TY-PZ-14-OPT]]></BLineCode>
<BLineName><![CDATA[Perl]]></BLineName>
<BLineDesc><![CDATA[]]></BLineDesc>
<BLineSeverity><![CDATA[1]]></BLineSeverity>
<IsCheck><![CDATA[1]]></IsCheck>
<RuleExpression><![CDATA[on remote_record it Match
YES]]></RuleExpression>
<BLineValue><![CDATA[]]></BLineValue>
<Unit><![CDATA[]]></Unit>
<FormatRule><![CDATA[Linux]]></FormatRule>
<Solution><![CDATA[]]></Solution>
<BLineStandard><![CDATA[1]]></BLineStandard>
</Rule>
<Rule>
<BLineCode><![CDATA[JX-LINUX-TY-WW-16-OPT]]></BLineCode>
<BLineName><![CDATA[Perl]]></BLineName>
<BLineDesc><![CDATA[]]></BLineDesc>
<BLineSeverity><![CDATA[1]]></BLineSeverity>
<IsCheck><![CDATA[1]]></IsCheck>
<RuleExpression><![CDATA[on remote_record it Match
YES]]></RuleExpression>
<BLineValue><![CDATA[]]></BLineValue>
<Unit><![CDATA[]]></Unit>
<FormatRule><![CDATA[Linux]]></FormatRule>
<Solution><![CDATA[]]></Solution>
<BLineStandard><![CDATA[1]]></BLineStandard>
</Rule>
</ScriptRule>
XML
my @nodes = $doc->getElementsByTagName("BLineCode");
for my $node (@nodes) {
my $cdata = $node->getFirstChild;
print $node->getTagName, " => ", $cdata->getData, "\n";
}
2011/3/11 DXX <[email protected]>
> 这个上面已经找了,只有一个小例子,所有的方法解释的相当的简单且没有例子,google了一下,发现网上所有的无论博客还是论坛,内容都是一模一样的,都是perldoc
> XML::DOM的前十行,我都汗死了
>
>
> 2011/3/11 Michael Zeng <[email protected]>
>
>> search.cpan.org 上 看看
>>
>>
>>
>> 2011/3/11 DXX <[email protected]>
>>
>>> 恩,是的,用正则可以,但是上面的头指明了必须要用dom来做,比较棘手,dom的资料太不好找了
>>>
>>> 2011/3/11 Michael Zeng <[email protected]>
>>>
>>> 抛开xml及其模块, 用regex 也可以做啊
>>>>
>>>> ( my $data ) = $string =~
>>>> /^\s*BLineCode><!\[CDATA\[(.*)\]\]><\/BLineCode>\s*$/m ;
>>>>
>>>>
>>>> print $data ;
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> 2011/3/11 DXX <[email protected]>
>>>>
>>>>>
>>>>> hi:
>>>>>
>>>>> 我是一个perl程序员,最近遇到一个棘手的问题,我们有个脚本(解析XML文件)现在要用dom模块来进行解析,但是相关的文档太少了,我看了dom的源码,找了google还是没找到到底该怎么解决,所以希望路过的大神能够看到这个问题,并顺手解决一下:
>>>>> XML文件是这样的:
>>>>> <ScriptRule>
>>>>> <Rule>
>>>>> <BLineCode><![CDATA[JX-LINUX-TY-PZ-14-OPT]]></BLineCode>
>>>>> <BLineName><![CDATA[Perl]]><<
>>>>> /BLineName>
>>>>> <BLineDesc><![CDATA[]]></BLineDesc>
>>>>> <BLineSeverity><![CDATA[1]]></BLineSeverity>
>>>>> <IsCheck><![CDATA[1]]></IsCheck>
>>>>> <RuleExpression><![CDATA[on remote_record it Match
>>>>> YES]]></RuleExpression>
>>>>> <BLineValue><![CDATA[]]></BLineValue>
>>>>> <Unit><![CDATA[]]></Unit>
>>>>> <FormatRule><![CDATA[Linux]]></FormaatRule>
>>>>> <Solution><![CDATA[]]></Solution>
>>>>> <BLineStandard><![CDATA[1]]></BLineStandard>
>>>>> </Rule>
>>>>> </ScriptRule>
>>>>>
>>>>> 我现在要将cdata进行解析,并放入一个哈希或者数组中:
>>>>> 形式像这样的:
>>>>> %hash =
>>>>> ("BLineCode"=>"JX-LINUX-TY-PZ-14-OPT"),或者是简单的告诉我要怎么把BLineCode和对应的cdata值取出来,不盛感激。
>>>>> 期待有个传说的人物出现,解决我这个问题
>>>>> --
>>>>> nothing is impossible to a willing heart
>>>>>
>>>>> --
>>>>> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
>>>>> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
>>>>> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
>>>>> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Yours Sincerely
>>>> Zeng Hong
>>>>
>>>> --
>>>> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
>>>> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
>>>> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
>>>> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>>>>
>>>
>>>
>>>
>>> --
>>> nothing is impossible to a willing heart
>>>
>>> --
>>> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
>>> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
>>> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
>>> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>>>
>>
>>
>>
>> --
>> Yours Sincerely
>> Zeng Hong
>>
>> --
>> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
>> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
>> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
>> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>>
>
>
>
> --
> nothing is impossible to a willing heart
>
> --
> 您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
> 要向此网上论坛发帖,请发送电子邮件至 [email protected]。
> 要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
> 若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。
>
--
您收到此邮件是因为您订阅了 Google 网上论坛的“PerlChina Mongers 讨论组”论坛。
要向此网上论坛发帖,请发送电子邮件至 [email protected]。
要取消订阅此网上论坛,请发送电子邮件至 [email protected]。
若有更多问题,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问此网上论坛。