码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • perl 用 XML::Parser 解析 XML文件,访问哈希


    本篇我们会看到 Perl 成为知名编程语言的关键特色--哈希 hash(2000年以前叫:关联数组)。

    在Perl 中,可以使用各种模块和函数来解析 XML元素和属性。其中,最古老的模块是  XML::Parser,它提供了一组完整的XML解析和处理函数,可以解析XML文档中的元素和属性。

    例如,下面是一个使用 XML::Parser 模块解析 XML元素和属性 的示例代码:

    编写 xml_parser_tree.pl  如下

    1. #!/usr/bin/perl
    2. use 5.010;
    3. use strict;
    4. use warnings;
    5. use utf8;
    6. use XML::Parser;
    7. use Data::Dumper;
    8. if ($#ARGV != 0){
    9. die "You must specify a file.xml to parse";
    10. }
    11. my $file = shift @ARGV;
    12. # Tree 风格比较难用,它的数据结构不符合标准的JSON.
    13. my $p = XML::Parser->new(Style => 'Tree',
    14. Handlers => {Start => \&start, End => \&end_, Char => \&text});
    15. my $tree = $p->parsefile($file)
    16. or die "cannot read file.xml\n";
    17. #print Dumper($tree);
    18. my $f2 = $file .'.txt';
    19. # 写入文件
    20. open(my $fw, '>:encoding(UTF-8)', $f2) or die "cannot open file '$f2' $!";
    21. my @array;
    22. # 访问 hash
    23. sub start {
    24. my ($self, $tag, %attribs) = @_;
    25. if ($tag eq 'node'){
    26. push @array, $attribs{'TEXT'};
    27. }
    28. }
    29. sub end_ {
    30. my ($self, $tag) = @_;
    31. }
    32. sub text {
    33. my ($self, $text) = @_;
    34. }
    35. my $ln =0; # 行数
    36. foreach my $txt (@array){
    37. print $fw $txt ."\n";
    38. $ln++;
    39. }
    40. close($fw);
    41. print $ln;

    运行 perl xml_parser_tree.pl your_test.xml

    编写  xml_parser_subs.pl  如下

    1. #!/usr/bin/perl
    2. use 5.010;
    3. use strict;
    4. use warnings;
    5. use utf8;
    6. use XML::Parser;
    7. #use Data::Dumper;
    8. if ($#ARGV != 0){
    9. die "You must specify a file.xml to parse";
    10. }
    11. my $file = shift @ARGV;
    12. # Subs 风格比较容易使用,它需要对应于标签名定义子程序
    13. my $p = XML::Parser->new(Style => 'Subs',
    14. Handlers => {Char => \&text});
    15. my $doc = $p->parsefile($file)
    16. or die "cannot read file.xml\n";
    17. say '$doc is a ', $doc;
    18. my $f2 = $file .'.txt';
    19. # 写入文件
    20. open(my $fw, '>:encoding(UTF-8)', $f2) or die "cannot open file '$f2' $!";
    21. my @array;
    22. # 访问 hash
    23. sub node {
    24. my ($self, $tag, %attribs) = @_;
    25. push @array, $attribs{'TEXT'};
    26. }
    27. sub node_ {
    28. my ($self, $tag) = @_;
    29. }
    30. sub text {
    31. my ($self, $text) = @_;
    32. }
    33. my $ln =0; # 行数
    34. foreach my $txt (@array){
    35. print $fw $txt ."\n";
    36. $ln++;
    37. }
    38. close($fw);
    39. print $ln;

    运行 perl xml_parser_subs.pl your_test.mm

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

  • 相关阅读:
    git随记
    pulsar起client客户端时(client,producer,consumer)各个配置
    UMA Frame Buffer Size 核显显存与CSGO帧率
    算法通关村第二关-白银挑战反转链表拓展问题
    通过LiveGo学习WebSocket
    通过java代码在指定目录生成一个指定内容的vue文件
    【九章斩题录】Leetcode:判定是否互为字符重排(C/C++)
    全景应用程序监控
    beego语言golang编程语言安装bee : 无法将“bee”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。
    分布式锁的3种实现!附代码
  • 原文地址:https://blog.csdn.net/belldeep/article/details/136759388
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号