设为首页 收藏本站
查看: 975|回复: 0

[经验分享] perl XML::Parser小介绍

[复制链接]

尚未签到

发表于 2018-8-31 09:14:56 | 显示全部楼层 |阅读模式
  1.概念:
  XML是一种Extensible Mark-up Language,事实上他不是一种mark-up语言,他
  是一种用来定义更适合特定tasks的新mark-up语言的方法。他的工作原理是:为
  DTDs(Document Type Definitions)定义了语法。一个DTD定义了文档中可以出
  现的一系列的元素,元素的属性以及各元素之间的关系;他还定义了元素的强制
  性与可选性,元素的顺序,元素之间的包含关系(是否强制等)。
  XML更专注的是每个数据元素是什么,而不是数据元素在网页中如何显示。
  2.XML基本规则:
  XML的正确性表达有两种方式:valid和well-formed
  下面是well-formed的规则:
  1)XML文档必须有一个顶级element。
  2)所有的元素必须有打开和关闭tags(除了特殊的空tags,在空tags实例中打
  开tags也会作为closeing tags)。
  3)打开和关闭tags必须有正确的嵌套。
  4)所有的属性必须被引用并且不能包含"new(Style => 'Stream');
  $p->parsefile(shift);
  print "Outlook: $forecast{outlook}\n";
  foreach(keys %forecast) {
  next if /outlook/;
  print "$_:$forecast{$_}->{val} $forecast{$_}->{deg}\n";
  }
  sub StartTag {
  my($p,$tag) = @_;
  push @curr,$tag;
  if ($tag eq 'TEMPERATURE') {
  $type = $_{TYPE};
  $forecast{$type}->{deg} = $_{DEGREES};
  }
  }
  sub EndTag {
  pop @curr;
  }
  sub Text {
  my($p) = shift;
  return unless /\S/;
  s/^\s+//;
  s/\s+$//;
  if ($curr[-1] eq 'OUTLOOK') {
  $forecast{outlook} .= $_;
  } elsif ( $curr[-1] eq 'TEMPERATURE' ) {
  $forecast{$type}->{val} = $_;
  }
  }
  print "-" x 9,"\n";
  foreach my $key (keys %forecast) {
  print "$key\n";
  }
  print "-" x 9,"\n";
  foreach my $value (values %forecast) {
  print "$value\n";
  }
  print "-" x 9,"\n";
  [root@dou xml]#
  执行后的结果:
  [root@dou xml]# perl sample1.pl sample1
  Outlook: Partly Cloudy
  MIN:6 C
  MAX:12 C
  ---------
  MIN
  outlook
  MAX
  ---------
  HASH(0x1c78d7b0)
  Partly Cloudy
  HASH(0x1c78d6b0)
  ---------
  [root@dou xml]#
  针对脚本进行解析:
  my $p = XML::Parser->new(Style => 'Stream')---->
  new构造函数是XML::Parser的一个class method。传递给该函数的参数是keyword/value 对。
  Styles中的Stream:
  This style also uses the Pkg package. If none of the subs that this style looks for is there, then the effect of parsing with this style is to print a canonical copy of the document without comments or declarations. All the subs receive as their 1st parameter the Expat instance for the document they're parsing.
  It looks for the following routines:

  • StartDocument  Called at the start of the parse .
  • StartTag  Called for every start tag with a second parameter of the element type. The $_ variable will contain a copy of the tag and the %_ variable will contain attribute values supplied for that element.
  sub StartTag {
  my($p,$tag) = @_;
  .......
  }
  所有的子函数的第一个参数都是Expat instance,对于StartTag函数他的第二个参数是元素类型,然后对于该元素的属性保存在一个%_哈希中。
  因此,我们可以通过操作哈希来操作元素对应的属性:
  $type = $_{TYPE};
  $forecast{$type}->{deg} = $_{DEGREES};
  因为:TYPE="MAX" DEGREES="C"相当于一个如下的hash的键值对:
  {
  TYPE => MAX,
  DEGREES => C,
  }

  • EndTag  Called for every end tag with a second parameter of the element type. The $_ variable will contain a copy of the end tag.
  • Text  Called just before start or end tags with accumulated non-markup text in the $_ variable.
  • PI  Called for processing instructions. The $_ variable will contain a copy of the PI and the target and data are sent as 2nd and 3rd parameters respectively.
  • EndDocument  Called at conclusion of the parse.


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-559181-1-1.html 上篇帖子: 如何提高perl处理大文件的效率 下篇帖子: perl XML::Parser
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表