Reputation: 50
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
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
Reputation: 8332
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