Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 26th, 2011, 05:04 AM
Rafay Rafay is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 5 Rafay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 10 sec
Reputation Power: 0
Help needed to get (Key,Value) from XML based on previous element (Key,Value) data

Based on the below sample xml, I need to write a perl script which will fetch all ‘TOTALAMOUNT’ value, however I also need to put a condition on previous element key ‘INVOICEDATE’ based on which ‘TOTALAMOUNT’ can be retrived.

<INVOICE INVOICENUMBER="007" INVOICEDATE="20110302" >
<TOTALAMTINTAX TOTALAMOUNT="1,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="008" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="1,47 "/> </INVOICE>

<INVOICE INVOICENUMBER="009" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="5,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="010" INVOICEDATE="20110502">
<TOTALAMTINTAX TOTALAMOUNT="6,27 "/> </INVOICE>

Reply With Quote
  #2  
Old May 26th, 2011, 05:11 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
What have you tried so far?
__________________
--Ax
without exception, there is no rule ...
Handmade Irish Jewellery
Targeted Advertising Cookie Optout (TACO) extension for Firefox
The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones


09 F9 11 02
9D 74 E3 5B
D8 41 56 C5
63 56 88 C0
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
-- Jamie Zawinski
Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE

Reply With Quote
  #3  
Old May 26th, 2011, 05:51 AM
Rafay Rafay is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 5 Rafay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 10 sec
Reputation Power: 0
code tried till now.

Quote:
Originally Posted by Axweildr
What have you tried so far?

I have tried to below code, it is working, but i need to put some condition somewhere for previous element key ‘INVOICEDATE’ .

# initialize the parser
my $parser = XML::Parser->new( Handlers =>
{
Start=>\&handle_start
});
foreach $file (@ecmfiles)

{
# Parse the XML file
$parser->parsefile( $file );

# Process a start-of-element event: print message about element
sub handle_start
{
my( $expat, $element, %attrs ) = @_;
if($element =~ /TOTALAMTINTAX/)
{
if( %attrs )
{
while( my( $key, $value ) = each( %attrs ))
{
if($key =~ /TOTALAMOUNT/)
{
print "\t$key => $value\n";
$value =~ s/,/./g;
print LOGFILE "Value : $value \n";
push( @ecm_amt_arr, $value );
}
}
}
}
}
}

Reply With Quote
  #4  
Old May 26th, 2011, 06:15 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
Try this ;

perl Code:
Original - perl Code
  1.  
  2. # initialize the parser
  3. my $parser = XML::Parser->new(
  4.                                 Handlers => {
  5.                                     Start=>\&handle_start
  6.                                 }
  7.                             );
  8. foreach $file (@ecmfiles) {
  9.    # Parse the XML file
  10.    $parser->parsefile( $file );
  11.    # Process a start-of-element event: print message about element
  12.    sub handle_start {
  13.         my( $expat, $element, %attrs ) = @_;
  14.         if($element =~ /TOTALAMTINTAX/) {
  15.             if( %attrs ) {
  16.                 $inv_date="";
  17.                 while( my( $key, $value ) = each( %attrs )) {
  18.                     if($key =~ /TOTALAMOUNT/) {
  19.                         print "\t$key => $value || Date => $inv_date\n";
  20.                         $value =~ s/,/./g;
  21.                         print LOGFILE "Value : $value \n";
  22.                         push( @ecm_amt_arr, $value );
  23.                     } else {
  24.                         if ($key =~ /INVOICEDATE/) {
  25.                             $inv_date=$value;
  26.                         }
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }

Reply With Quote
  #5  
Old May 26th, 2011, 06:37 AM
miller miller is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 104 miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 20 h 5 m 59 sec
Reputation Power: 179
Using XML::Twig:

Code:
use XML::Twig;

use strict;
use warnings;

my $xml = do {local $/; <DATA>};

my $twig = XML::Twig->new();
$twig->parse($xml);

for my $node ($twig->findnodes(q{//INVOICE[@INVOICEDATE]})) {
	my $dateatt = $node->att('INVOICEDATE');
	next if $dateatt < 20110401;
	for my $taxnode ($node->findnodes(q{TOTALAMTINTAX})) {
		my $amtatt = $taxnode->att('TOTALAMOUNT');
		print $amtatt, "\n";
	}
}

__DATA__
<root>
<INVOICE INVOICENUMBER="007" INVOICEDATE="20110302" >
<TOTALAMTINTAX TOTALAMOUNT="1,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="008" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="1,47 "/> </INVOICE>

<INVOICE INVOICENUMBER="009" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="5,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="010" INVOICEDATE="20110502">
<TOTALAMTINTAX TOTALAMOUNT="6,27 "/> </INVOICE>
</root>

Reply With Quote
  #6  
Old May 26th, 2011, 06:45 AM
Rafay Rafay is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 5 Rafay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 10 sec
Reputation Power: 0
In the xml invoice date is already there,

I need something like this,

For each element in xml file
{
If (element = INVOICE)
{
Get ‘INVOICEDATE’ key and its value;
If (INVOICEDATE value == 20110402)
{
Get child elements, If (child element == TOTALAMTINTAX)
{
Get (key,value) TOTALAMOUNT;
}
}
}
}

Reply With Quote
  #7  
Old May 26th, 2011, 07:12 AM
miller miller is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 104 miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 20 h 5 m 59 sec
Reputation Power: 179
That's exactly what i just demonstrated.

- M

Reply With Quote
  #8  
Old May 26th, 2011, 07:50 AM
Rafay Rafay is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 5 Rafay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by miller
That's exactly what i just demonstrated.

- M

Thanks Miller,
I was referring to the other code.
But Alas while using twig, getting error,

Can't locate XML/Twig.pm in @INC .

I am using client machine Solaris....

Reply With Quote
  #9  
Old May 26th, 2011, 08:04 AM
Axweildr's Avatar
Axweildr Axweildr is offline
'fie' on me, allege-dly
Click here for more information.
 
Join Date: Mar 2003
Location: in da kitchen ...
Posts: 12,874 Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)Axweildr User rank is General 81st Grade (Above 100000 Reputation Level)  Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1Folding Points: 162285 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 4 Months 2 Weeks 1 Day 20 h 28 m 56 sec
Reputation Power: 6421
Send a message via Google Talk to Axweildr
Orkut
command line
perl -MCPAN -e shell
cpan> install XML::Twig

Reply With Quote
  #10  
Old May 26th, 2011, 08:55 AM
Rafay Rafay is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 5 Rafay User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by Axweildr
command line
perl -MCPAN -e shell
cpan> install XML::Twig


Tried it, but connection is failing with http server
---------------------------------------------------------
Getting the below message
---------------------------------------------------------
Could not fetch modules/03modlist.data.gz
Going to write /u01/pin54/.cpan/Metadata
Warning: Cannot install XML::Twig, don't know what it is.

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

can we do something with xml::Parser?

Reply With Quote
  #11  
Old May 27th, 2011, 10:34 PM
miller miller is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 104 miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level)miller User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 20 h 5 m 59 sec
Reputation Power: 179
If you have to use XML::Parser, the following would do the job. I'm a much bigger fan of XML::Twig though if given a choice between the two modules.

Code:
use XML::Parser;

use strict;
use warnings;

my $xml = do {local $/; <DATA>};

my $mindate = 20110401;
my $matches = 0;

my $p = XML::Parser->new(Handlers => {
	Start => sub {
		my ($expat, $element, %attr) = @_;
		if ($element eq 'INVOICE') {
			$matches = $attr{INVOICEDATE} >= $mindate;
		} elsif ($element eq 'TOTALAMTINTAX') {
			print "$attr{TOTALAMOUNT}\n" if $matches;
		}
	},
});
$p->parse($xml);

__DATA__
<root>
<INVOICE INVOICENUMBER="007" INVOICEDATE="20110302" >
<TOTALAMTINTAX TOTALAMOUNT="1,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="008" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="1,47 "/> </INVOICE>

<INVOICE INVOICENUMBER="009" INVOICEDATE="20110402">
<TOTALAMTINTAX TOTALAMOUNT="5,27 "/> </INVOICE>

<INVOICE INVOICENUMBER="010" INVOICEDATE="20110502">
<TOTALAMTINTAX TOTALAMOUNT="6,27 "/> </INVOICE>
</root>


- Miller

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Help needed to get (Key,Value) from XML based on previous element (Key,Value) data

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap