1

I was not able to parse the xml data properly. I need your help.

**Code**
#!usr/bin/perl
use strict;
use warnings;
open(FILEHANDLE, "data.xml")|| die "Can't open";
my @line;
my @affi;

my @lines;
my $ct =1 ;
print "Enter the start position:-";

my $start= <STDIN>;
print "Enter the end position:-";


my $end = <STDIN>;

print "Processing your data...\n";
my $i =0;
my $t =0;
while(<FILEHANDLE>)
{
    if($ct>$end)
    {
       close(FILEHANDLE);
       exit;
       
    }
    if($ct>=$start)
    {
       $lines[$t] = $_;
       $t++;
     }
     
     if($ct == $end)
     {
    my $i = 0;
    my $j = 0;
    my @last;
    my @first;
    my $l = @lines;
    my $s = 0;

while($j<$l)
{
    if ($lines[$j] =~m/@/)
    {
        $line[$i] = $lines[$j];
        $u = $j-3;
        $first[$i]=$lines[$s]; 
        $s--;
        $last[$i] = $lines[$u];
        #$j = $j+3;
        #$last[$i]= $lines[$j];
        #$j++;
        #$first[$i] = $lines[$j];
        $i++;
    }
$j++;
}
my $k = 0;
foreach(@line)
{
  $line[$k] =~ s/<.*>(.* )(.*@.*)<.*>/$2/;
  $affi[$k] = $1;
  $line[$k] = $2;
    $line[$k] =~ s/\.$//;
    
    
    $k++;
  }
my $u = 0;
foreach(@first)
{
  $first[$u] =~s/<.*>(.*)<.*>/$1/;
  $first[$u]=$1;  
  $u++;
  }
my $m = 0;
foreach(@last)
{
  $last[$m] =~s/<.*>(.*)<.*>/$1/;
  $last[$m] = $1;    
  $m++;
  }
my $q=@line;
open(FILE,">Hayathi.txt")|| die "can't open";
my $p;

for($p =0; $p<$q; $p++)
{  
  print FILE "$line[$p]  $last[$p],$first[$p]   $affi[$p]\n";  
} 

close(FILE);
     }
     
  
  $ct++;
  }

This code should extract lastName firstName and affiliation from the data and should save in a text file.

I have tried the above code, but I was not able to get the firstName in the output. I request you to please help me by correcting the code. Thank you in advance.

3
  • 3
    Start by using an XML parser module. Commented Dec 26, 2022 at 2:47
  • 2
    Can you include the XML input file as a text file (not image)? Then we can more easily copy and paste it Commented Dec 26, 2022 at 9:11
  • Might you please edit your question to include a sample of the XML you need to parse (as text, not as a screen shot)? For instructions on formatting see How do I format my code blocks?. Absent a minimal reproducible example that includes some XML, it will be hard to answer your question. See How to Ask. - From Review. Commented Dec 28, 2022 at 2:43

1 Answer 1

1

You can take following code sample as basis of your code.

As no text xml sample data file provided the help is very limited based on data image.

Documentation: XML::LibXML

use strict;
use warnings;
use feature 'say';

use XML::LibXML;

my $file = 'europepmc.xml';

my $dom = XML::LibXML->load_xml(location => $file);

foreach my $node ($dom->findnodes('//result')) {
    say 'NodeID:    ', $node->{id};
    say 'FirstName: ', $node->findvalue('./firstName');
    say 'LastName:  ', $node->findvalue('./lastName');
    say '';
}

exit 0;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the code, Sir. Could you please add the procedure to export the output to a text file in csv format.
@ravi.gteja You'd just need to adjust the say part to instead print what you want. You should be able to do that yourself; it's the easy part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.