perl 用 XML::Parser 解析 XML文件,访问哈希

本篇我们会看到 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';
# 写入文件
open(my $fw, '>:encoding(UTF-8)', $f2) or die "cannot open file '$f2' $!";
my @array;
# 访问 hash
sub start { 
    my ($self, $tag, %attribs) = @_;
    if ($tag eq 'node'){
        push @array, $attribs{'TEXT'};
    }
}
sub end_ {
    my ($self, $tag) = @_;
}
sub text {
    my ($self, $text) = @_;
}
my $ln =0; # 行数
foreach my $txt (@array){
    print $fw $txt ."\n";
    $ln++;
}
close($fw);
print $ln;

运行 perl xml_parser_tree.pl your_test.xml

编写  xml_parser_subs.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;
# Subs 风格比较容易使用,它需要对应于标签名定义子程序
my $p = XML::Parser->new(Style => 'Subs',
        Handlers => {Char => \&text});
my $doc = $p->parsefile($file) 
            or die "cannot read file.xml\n";
say '$doc is a ', $doc;

my $f2 = $file .'.txt';
# 写入文件
open(my $fw, '>:encoding(UTF-8)', $f2) or die "cannot open file '$f2' $!";
my @array;
# 访问 hash
sub node { 
    my ($self, $tag, %attribs) = @_;
    push @array, $attribs{'TEXT'};
}
sub node_ {
    my ($self, $tag) = @_;
}
sub text {
    my ($self, $text) = @_;
}
my $ln =0; # 行数
foreach my $txt (@array){
    print $fw $txt ."\n";
    $ln++;
}
close($fw);
print $ln;

运行 perl xml_parser_subs.pl your_test.mm

参阅:XML::Parser - A perl module for parsing XML documents - metacpan.org

相关推荐

  1. perl XML::Parser 解析 XML文件访问

    2024-03-16 22:56:01       20 阅读
  2. perl XML::LibXML 解析 Freeplane.mm文件

    2024-03-16 22:56:01       17 阅读
  3. OpenSSL生成密钥

    2024-03-16 22:56:01       36 阅读
  4. C#加密和解密

    2024-03-16 22:56:01       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-16 22:56:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-16 22:56:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-16 22:56:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-16 22:56:01       20 阅读

热门阅读

  1. redis的过期策略以及内存淘汰机制

    2024-03-16 22:56:01       23 阅读
  2. 【Android】TextView前增加红色必填项星号*

    2024-03-16 22:56:01       20 阅读
  3. Vue3.0+vite vite.config.ts配置与env

    2024-03-16 22:56:01       18 阅读
  4. 【嵌入式——QT】线程同步

    2024-03-16 22:56:01       20 阅读
  5. Qt是什么?

    2024-03-16 22:56:01       19 阅读
  6. 第1章第2节:SAS语言基础

    2024-03-16 22:56:01       23 阅读
  7. 3月16日ACwing每日一题

    2024-03-16 22:56:01       19 阅读
  8. View UI清除表单

    2024-03-16 22:56:01       21 阅读
  9. 构建专业聊天软件:C#编程深度解析

    2024-03-16 22:56:01       22 阅读
  10. 树莓派开机自动播放U盘里的照片和视频

    2024-03-16 22:56:01       26 阅读