Help in parsing XML output file in perl.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in parsing XML output file in perl.
# 1  
Old 12-17-2012
Question Help in parsing XML output file in perl.

Hi I have an XML output like :
Code:
  <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <envelope>
- <body>
- <outputGetUsageSummary>
- <usgSumm rerateDone="5">
- <usageAccum accumId="269" accumCaptn="VD_DP_AR" inclUnits="9999999.00" inclUnitsUsed="0.00" shared="false" pooled="false" prorated="false" realtimeSbscrpShared="false" inclUnitsRolledOver="0.00" maxRolloverUnits="0.00" inclUnitsToBeRolledOver="0.00" rolloverAvailableFlag="false" prevCycleReleasedFlag="false" sbscrpUsedUnits="0.00">
  <prices svcPriceSeqNbr="00000RH2LS808H7" svcName="VD_DP_AR" captnText="Vm Deposit Free Airtime" inclUnits="9999999.00" inclUnitsUsed="0.00" ntwrkUnitsIncl="599999940.00" ntwrkUnitsUsed="0.00" maxAvailNtwrkUnits="599999940.00" displayUnitsIncl="9999999.20" displayUnitsUsed="0.00" maxAvailDisplayUnits="9999999.20" ntwrkUnitId="1" displayUnitId="3" /> 
  </usageAccum>
- <usageAccum accumId="270" accumCaptn="VD_IN_AR" inclUnits="9999999.00" inclUnitsUsed="0.00" shared="false" pooled="false" prorated="false" realtimeSbscrpShared="false" inclUnitsRolledOver="0.00" maxRolloverUnits="0.00" inclUnitsToBeRolledOver="0.00" rolloverAvailableFlag="false" prevCycleReleasedFlag="false" sbscrpUsedUnits="0.00">
  <prices svcPriceSeqNbr="00000RH2LT888H8" svcName="VD_IN_AR" captnText="Vm Deposit Free Airtime" inclUnits="9999999.00" inclUnitsUsed="0.00" ntwrkUnitsIncl="599999940.00" ntwrkUnitsUsed="0.00" maxAvailNtwrkUnits="599999940.00" displayUnitsIncl="9999999.20" displayUnitsUsed="0.00" maxAvailDisplayUnits="9999999.20" ntwrkUnitId="1" displayUnitId="3" /> 
  </usageAccum>
  </usgSumm>
  </outputGetUsageSummary>
  </body>
  </envelope>

The XML output will be stored in a file called $output:
system("$resub_path -stdin < $input_xml_file >$output");


I need to grep the values of the fields 'inclUnits' ,'inclUnitsUsed','shared' (in Bold)for each accumId i.e., for '269','270' from the output file.
I have these values stored in an array ('269','270').
Can anyone help me in grepping the above mentioned corresponding filelds for each accumid from the $output file and store them in variables?

Thanks in advance
# 2  
Old 12-17-2012
You are better off using XML::Parser - search.cpan.org
to parse xml.

It is somewhat complex, but it will handle correctly formed XML strings.
What if for some valid reason some "field" you expect to key off of is missing? grep may break. An xml parser won't.
# 3  
Old 12-18-2012
Question

Quote:
Originally Posted by jim mcnamara
You are better off using XML::Parser - search.cpan.org
to parse xml.

It is somewhat complex, but it will handle correctly formed XML strings.
What if for some valid reason some "field" you expect to key off of is missing? grep may break. An xml parser won't.
Is there anyway I can use grep for fetching those fields from output.
I don't need all those values to be fetched.
let's say from the output,I need to fetch the fields for only accumId='269' i.e., I need to fetch the values 'inclUnits','inclUnitsUsed','shared' for accumId='269' and need to store them in three different variables.
is it possible to do so ?Please help me if it can be done with grep.

Thanks in Advance
# 4  
Old 12-18-2012
Try this:
Code:
awk -F= '/(accumId="269"|accumId="270")/ {
 unit=$4; gsub(/(\"|inclUnitsUsed)/,"",unit);
 used=$5; gsub(/(\"|shared)/,"",used);
 shrd=$6; gsub(/(\"|pooled)/,"",shrd);
 printf "%s %s %s\n", unit, used, shrd;
 } ' xmlfile

This User Gave Thanks to Yoda For This Post:
# 5  
Old 12-19-2012
I have just used the code you have given in a sample perl file,while compiling it is throwing some error,I'm unable to figure out them.can you please help me fixing these errors:

Code:
#!perl
use Oraperl;
use DBI;
use DBD::Oracle qw(:ora_types);
use Time::Local;

use vars qw($opt_f);
&Getopts('f:');

if (!defined( $opt_f ))
{
	print "Test.pl - Missing filename.\n ";
	exit -1;
}
print "\n $opt_f ";
awk -F= '/(accumId="450")/ {
 unit=$4; gsub(/(\"|inclUnitsUsed)/,"",unit);
 used=$5; gsub(/(\"|shared)/,"",used);
 shrd=$6; gsub(/(\"|pooled)/,"",shrd);
 printf  "%s %s %s\n", unit, used, shrd";
 } ' $opt_f  ;

    print "\n $unit .";
    print "\n $used .";
    print "\n $shrd .";

While compiling it I'm getting these errors:
Code:
 Test.pl outexg2.xml
Scalar found where operator expected at ./Test.pl line 21, near "} ' $opt_f"
  (Might be a runaway multi-line '' string starting on line 16)
        (Missing operator before  $opt_f?)
syntax error at ./Test.pl line 21, near "} ' $opt_f  "
Execution of ./Test.pl aborted due to compilation errors.

# 6  
Old 12-19-2012
perl

Hi,

Try this one,

Code:
open(FILE,"<","$xml_file_name");


while(<FILE>){
    if($_ =~ m/accumId="(269|270)"/) {
        my $accumId = $1;
        my @inclUnits=split(/ /,$_);
        my ($inclUnits)=grep(/inclUnits=/, @inclUnits);
        $inclUnits =~ s/inclUnits="([^"]+)"/$1/g;
        my ($inclUnitsUsed)=grep(/inclUnitsUsed=/, @inclUnits);
        $inclUnitsUsed =~ s/inclUnitsUsed="([^"]+)"/$1/g;
        my ($shared)=grep(/shared=/, @inclUnits);
        $shared =~ s/shared="([^"]+)"/$1/g;
        print "accumId=$accumId : $inclUnits : $inclUnitsUsed : $shared\n";
    }
}
close(FILE);

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 7  
Old 12-19-2012
Thankyou verymuch Smilie
It worked for meSmilie
and one more thing...can I be able to perform integer operations like division on these fetched values? or need to do typecast or something like that ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with parsing xml file

Hi, Need help with parsing xml data in unix and place it in a csv file. My xml file looks like this: <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <iwgroups> <nextid value="128"> </nextid> <iwgroup name="RXapproval" id="124" display-name="RXapproval"... (11 Replies)
Discussion started by: ajayakunuri
11 Replies

2. UNIX for Dummies Questions & Answers

Parsing XML file

I want to parse xml file sample file....... <name locale="en">my_name<>/name><lastChanged>somedate</lastChanged><some more code here> <name locale="en">tablename1<>/name><lastChanged>somedate</lastChanged> <definition><dbquery><sources><sql type="cognos">select * from... (10 Replies)
Discussion started by: ms2001
10 Replies

3. Shell Programming and Scripting

XML: parsing of the Google contacts XML file

I am trying to parse the XML Google contact file using tools like xmllint and I even dived into the XSL Style Sheets using xsltproc but I get nowhere. I can not supply any sample file as it contains private data but you can download your own contacts using this script: #!/bin/sh # imports... (9 Replies)
Discussion started by: ripat
9 Replies

4. Shell Programming and Scripting

parsing XML result by using perl?

for some reasons, i need to parse the XML result by using perl. for instance, this is a sample XML result: <Response> <status>success</status> <answer>AAA::AAA</answer> <answer>BBB::BBB</answer> </Response> then i can use this way : my @output = (); foreach my $parts (@all) ##@all... (2 Replies)
Discussion started by: tiger2000
2 Replies

5. Shell Programming and Scripting

parsing xml file

Hello! We need to parse weblogic config.xml file and display rows in format: machine:listen-port:name:application_name In our enviroment the output should be (one line for every instance): Crm-Test-Web:8001:PIA:peoplesoft Crm-Test-Web:8011:PIA:peoplesoft... (9 Replies)
Discussion started by: annar
9 Replies

6. Shell Programming and Scripting

Bash XML Parsing using Perl XPath

I have a bash script that needs to read input from an XML file, which includes varying numbers of a certain type of child node. I want to be able to iterate through all the child nodes of a given parent. I installed the Perl XML-XPath package from search.cpan.org. Once it's installed, from bash,... (4 Replies)
Discussion started by: jfmorales
4 Replies

7. Shell Programming and Scripting

Random XML Parsing - using Perl

Given the XML: <?xml version="1.0" encoding="UTF-8"?> <reference> <refbody> <section> <p> <ul> <li><xref href="file1.dita#anchor" /></li> <li><xref href="file2.dita#anchor" /></li> </ul> </p> </section> <section> <p> <xref href="file3.dita#anchor" /> </p> <p> <xref... (4 Replies)
Discussion started by: ricksj
4 Replies

8. UNIX for Dummies Questions & Answers

Help parsing a XML file ....

Well I have read several threads on the subject ... but being a newbie like me makes it hard to understand ... What I need is the following: Input data: ------- snip --------- <FavouriteLocations> <FavouriteLocations class="FavouriteList"><Item... (6 Replies)
Discussion started by: misak
6 Replies

9. UNIX for Advanced & Expert Users

xml parsing error in perl

******************PERL VERSION************************ This is perl, v5.8.1 built for i386-linux-thread-multi ERROR!!!!---Undefined subroutine &main::start called at /usr/lib/perl5/vendor_perl/5.8.0/i386-linux-thread-multi/XML/Parser/Expat.pm line 469. *********************PERL... (1 Reply)
Discussion started by: bishweshwar
1 Replies

10. Shell Programming and Scripting

HTTP Query Request & Parsing returned XML output

I have a PERL script from which I need to make a HTTP request to Web Servlet (Essentially a URL with variables and values like &Variable1=AAAAAA&Variable2=BBBBBBBBB&Variable3=CCCCCCC). The Web servlet returns an XML result which needs to be parsed for the contents of the result within the program.... (15 Replies)
Discussion started by: jerardfjay
15 Replies
Login or Register to Ask a Question