candy 发表于 2018-8-31 09:14:56

perl XML::Parser小介绍

  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";
  #
  执行后的结果:
  # perl sample1.pl sample1
  Outlook: Partly Cloudy
  MIN:6 C
  MAX:12 C
  ---------
  MIN
  outlook
  MAX
  ---------
  HASH(0x1c78d7b0)
  Partly Cloudy
  HASH(0x1c78d6b0)
  ---------
  #
  针对脚本进行解析:
  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]
查看完整版本: perl XML::Parser小介绍