Velmurugan
Velmurugan

Reputation: 50

Extract XML tags using Perl

I need a Perl script to separate XMl tags. For Example:

<bgtres>
 <resume key='267298871' score='5'>
 <xpath path='xpath://resume'>
 <resume canonversion='2' dateversion='2' present='734060'>........... </resume></xpath></resume>
</bgtres>

In this XML file i need to separate the things under the resume tag (inside the xpath), the resume tag that appears after the xpath should alone to extrated from a bundle of CV's. I need to do this in Perl Script.

Can anyone please give me an hint or coding to do this process. I need the Perl script for doing this process

Thanks in Advance

Upvotes: 1

Views: 6528

Answers (3)

sashank
sashank

Reputation: 1541

You need to use XML Parser in Perl.

Upvotes: -1

Bee
Bee

Reputation: 958

I am still new to perl and I am no expert at it. That said, I recently had to parse a XML file and I ended up using XML::DOM for it. Good thing I saw was that the code was still fairly easy to read when I had to go back to it after a month to add more functionality. Here is a small snippet to print the canonversion

use XML::DOM;
# Create instance of XML Dom Parser
my $parser = new XML::DOM::Parser;
# Read XML Doc
my $doc = $parser->parsefile ("$XMLFile");
# Fetch all resume tags
foreach my $resume ($doc->getElementsByTagName("resume")) {
    $canonversion = $resume->getAttributeNode("BuildName")->getValue;
    # Do something with it
    print $canonversion;
}

Hope that helps.

Upvotes: 3

Nikhil Jain
Nikhil Jain

Reputation: 8332

  • see XML::Twig - A perl module for processing huge XML documents in tree mode.
  • or XML::Simple - Easy API to maintain XML (esp config files)

like

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;

my $xml = q~<?xml version='1.0'?>
<bgtres>
 <resume key='267298871' score='5'>
  <xpath path='xpath://resume'>
   <resume canonversion='2' dateversion='2' present='734060'>
   </resume>
  </xpath>
 </resume>
</bgtres>~;

print $xml,$/;

my $data = XMLin($xml);

print Dumper( $data );

foreach my $test (keys %{$data->{resume}{xpath}{resume}}){
        print"$test : $data->{resume}{xpath}{resume}->{$test}\n";
}

Output:

<?xml version='1.0'?>
<bgtres>
 <resume key='267298871' score='5'>
  <xpath path='xpath://resume'>
   <resume canonversion='2' dateversion='2' present='734060'>
   </resume>
  </xpath>
 </resume>
</bgtres>
$VAR1 = {
          'resume' => {
                      'xpath' => {
                                 'resume' => {
                                             'dateversion' => '2',
                                             'canonversion' => '2',
                                             'present' => '734060'
                                           },
                                 'path' => 'xpath://resume'
                               },
                      'score' => '5',
                      'key' => '267298871'
                    }
        };
dateversion : 2
canonversion : 2
present : 734060

Upvotes: 5

Related Questions