English 中文(简体)
Perl XML 解析以获取数据
原标题:Perl XML parsing to get data
  • 时间:2012-05-25 10:59:08
  •  标签:
  • xml
  • perl

能否使用 Perl 脚本从这样的 XML 文件中只获取数据( 战争、 欧洲、 首先等), 并将其存储在. txt 文件中?

<Node id="93" /> 
  war 
  <Node id="102" /> 
  Europe 
  <Node id="112" /> 
  first 
  <Node id="117" /> 
  <Node id="118" /> 
  world
  <Node id="120" /> 
  <Node id="121" /> 
  country 
  <Node id="126" /> 
  fire
  <Node id="132" /> 
  <Node id="135" /> 
  The 
  <Node id="139" /> 
  Government
  <Node id="144" /> 
  has greeted 
  <Node id="157" /> 
  King
  <Node id="159" /> 
  <Node id="160" /> 
问题回答

您可以使用 < a href=> "http://p3rl.org/XML% 3a% 3aLibXML" rel=“ no follow” >XML::LibXML 。

use XML::LibXML qw();
my $dom = XML::LibXML->load_xml(string => << XML );
<root>
  <Node id="93" />
  war
  <Node id="102" />
  Europe
  <Node id="112" />
  first
  <Node id="117" />
  <Node id="118" />
  world
  <Node id="120" />
  <Node id="121" />
  country
  <Node id="126" />
  fire
  <Node id="132" />
  <Node id="135" />
  The
  <Node id="139" />
  Government
  <Node id="144" />
  has greeted
  <Node id="157" />
  King
  <Node id="159" />
  <Node id="160" />
</root>
XML

print $dom->textContent;

您可以使用"http://search.cpan.org/ ~grantm/XML-Sple-2.18/lib/XML/Spro.pm" rel =“nofollow”>XML::Spere 。 看起来你只想要内容部分。

use strict;
use warnings;
use XML::Simple;
my $xml = qq~<root>
<Node id="93" /> 
  war 
  <Node id="102" /> 
  Europe 
  <Node id="112" /> 
  first 
  <Node id="117" /> 
  <Node id="118" /> 
  world
  <Node id="120" /> 
  <Node id="121" /> 
  country 
  <Node id="126" /> 
  fire
  <Node id="132" /> 
  <Node id="135" /> 
  The 
  <Node id="139" /> 
  Government
  <Node id="144" /> 
  has greeted 
  <Node id="157" /> 
  King
  <Node id="159" /> 
  <Node id="160" />
</root>
~;

my $ref = XMLin($xml);
open $out,  > ,  output.txt  or die "Could not open output file";
print $out join "
", map {s/s{2,}//g;$_} @{$ref->{ content }};
close $out;

周围还有其他的 XML 模块, 您也可以使用 。

使用 < a href=> "http://p3rl.org/XML%3a%3aXSH2" rel="nofollow">XML::XSH2 :

open 1.xml ;
echo (/) ;




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签