Answer the question
In order to leave comments, you need to log in
A text file opens with one line, how to parse?
We need to parse the xml and extract ip addresses of the format 174.139.177.234 from it
while (defined(my $line = $fh->getline()))
{
if ( $line =~ m/(\d{1,3}\.\ d{1,3}\.\d{1,3}\.\d{1,3})<\/ip>/) {
print "$1\n";
}
}
But in perl xml opens in one line, and only the first match gets there, what to do?
Answer the question
In order to leave comments, you need to log in
If the input is XML, it's best to use the appropriate parser.
Here are good examples
. At the output of the parser there will be a hash - it remains to look for ip addresses in it.
Well, if it's wrong and in a quick way:
while (defined(my $line = $fh->getline())) {
while ( $line =~ m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})<\/ip>/g) {
print "$1\n";
}
}
It's better to use a parser. For example XML::LibXML:
use XML::LibXML;
use feature qw(say);
my $filename = "Sample.xml";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($filename);
my $root = $doc->getDocumentElement;
foreach my $node ( $doc->findnodes('//ip') ) {
my $value = $node->textContent();
say $value;
}
STATUS OF THIS MODULE
The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces. In particular, XML::LibXML is highly recommended.
The major problems with this module are the large number of options and the arbitrary ways in which these options interact - often with unexpected results.
Patches with bug fixes and documentation fixes are welcome, but new features are unlikely to be added.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question