本篇我们会看到 Perl 成为知名编程语言的关键特色--哈希 hash(2000年以前叫:关联数组)。
在Perl 中,可以使用各种模块和函数来解析 XML元素和属性。其中,最古老的模块是 XML::Parser,它提供了一组完整的XML解析和处理函数,可以解析XML文档中的元素和属性。
例如,下面是一个使用 XML::Parser 模块解析 XML元素和属性 的示例代码:
编写 xml_parser_tree.pl 如下
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use utf8;
use XML::Parser;
use Data::Dumper;
if ($#ARGV != 0){
die "You must specify a file.xml to parse";
}
my $file = shift @ARGV;
# Tree 风格比较难用,它的数据结构不符合标准的JSON.
my $p = XML::Parser->new(Style => 'Tree',
Handlers => {Start => \&start, End => \&end_, Char => \&text});
my $tree = $p->parsefile($file)
or die "cannot read file.xml\n";
#print Dumper($tree);
my $f2 = $file .'.txt';
# 写入文

最低0.47元/天 解锁文章
364

被折叠的 条评论
为什么被折叠?



