CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
reg. exp code to parse xml

 



niyas.mohd
New User

Nov 26, 2008, 12:38 AM

Post #1 of 2 (1937 views)
reg. exp code to parse xml Can't Post

Hi,
find the sample code
---------------------------------------------
#!c:\perl\bin

open(INPUTFILE, "+< Soap_Results.xml") or die "Unable to open sample.txt";
my (@rawdata, @validdata);
while(<INPUTFILE>)
{
my $Rawdata = $_;
if($Rawdata =~ /responseData class|\/(\w){2}\:ClaimID/)
{
push(@rawdata,"$Rawdata");
#$Rawdata = "$Rawdata";
}
}
close(INPUTFILE);

my $Arr1=join("\n",@rawdata);
$Arr1 =~ s/\&quot\;/\"/g;
$Arr1 =~ s/\&gt\;/\>/g;
$Arr1 =~ s/\&lt\;/\</g;
$Arr1 =~ s/\<responseData class\=\"java.lang.String\"\>/\~/g;
$count = 1;
@arr2 = split(/\~/, $Arr1);
foreach $elt (@arr2) {
if ($elt ne /^(\s+)$/) {
print "----$elt---";
$elt =~ /(\<q0\:ClaimID\>)(\w+)(\<\/q0\:ClaimID\>)/;
$val = $2;
$elt =~ s/(\<ClaimDenialDescription\>)(\<\/ClaimDenialDescription\>)/$1\<ClaimID\>$val\<\/ClaimID\>$2/g;
$elt =~ s/\<q0\:ClaimID\>(\w+)\<\/q0\:ClaimID\>//;
$elt =~ s/\<\/responseData\>//g;
open (OUTPUTFILE, "+> $count\.xml") or die "Unable to open sampleoutput.txt";
print OUTPUTFILE "$elt";
close(OUTPUTFILE);
$count ++;
}
}


---------------------------------------------

Code



      
    


FishMonger
Veteran / Moderator

Nov 26, 2008, 8:57 AM

Post #2 of 2 (1929 views)
Re: [niyas.mohd] reg. exp code to parse xml [In reply to] Can't Post

How about a few code comments.

First, using simple regexs to parse xml is a bad idea. You really should use an xml parser, such as:
XML::Twig
http://search.cpan.org/~mirod/XML-Twig-3.32/Twig.pm

or one of the many others
http://search.cpan.org/search?query=xml&mode=all

Your script is missing 2 very important items which should be in every Perl script you write.

use warnings;
use strict;


In Reply To
Hi,
find the sample code
---------------------------------------------
#!c:\perl\bin

open(INPUTFILE, "+< Soap_Results.xml") or die "Unable to open sample.txt";


If you want to follow the "Best Practices", it would be better to use a lexical var for the filehandle and the 3 arg form of open.

Why open the file in read/write mode when you only plan on reading?

You're opening "Soap_Results.xml" but the die statement says that you failed to open "sample.txt" and you didn't include the reason it failed.

Code
my $xml_file = 'Soap_Results.xml'; 
open my $XML, '<', $xml_file or die "Unable to open $xml_file $!";


In Reply To
my (@rawdata, @validdata);
while(<INPUTFILE>)
{
my $Rawdata = $_;

Is better written as:

Code
my (@rawdata, @validdata); 
while ( my $Rawdata = <$XML> ) {


In Reply To
if($Rawdata =~ /responseData class|\/(\w){2}\:ClaimID/)
{
push(@rawdata,"$Rawdata");
#$Rawdata = "$Rawdata";
}
}
close(INPUTFILE);



Code
push(@rawdata, $Rawdata);


You should read
perldoc -q quoting

There are other items that I could point out, but you get the gist.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives