The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Help needed to get (Key,Value) from XML based on previous element (Key,Value) data
Discuss Help needed to get (Key,Value) from XML based on previous element (Key,Value) data in the Perl Programming forum on Dev Shed. Help needed to get (Key,Value) from XML based on previous element (Key,Value) data Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 26th, 2011, 05:04 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
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>
|

May 26th, 2011, 05:11 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
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
Deta vil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ...
BIT COINS ANYONE
|

May 26th, 2011, 05:51 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
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 );
}
}
}
}
}
}
|

May 26th, 2011, 06:15 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
Try this ;
perl Code:
Original
- perl Code |
|
|
|
# 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 ) { $inv_date=""; while( my( $key, $value ) = each( %attrs )) { if($key =~ /TOTALAMOUNT/) { print "\t$key => $value || Date => $inv_date\n"; print LOGFILE "Value : $value \n"; push( @ecm_amt_arr, $value ); } else { if ($key =~ /INVOICEDATE/) { $inv_date=$value; } } } } } } }
|

May 26th, 2011, 06:37 AM
|
|
|
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>
|

May 26th, 2011, 06:45 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
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;
}
}
}
}
|

May 26th, 2011, 07:12 AM
|
|
|
|
That's exactly what i just demonstrated.
- M
|

May 26th, 2011, 07:50 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
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.... 
|

May 26th, 2011, 08:04 AM
|
 |
'fie' on me, allege-dly
|
|
Join Date: Mar 2003
Location: in da kitchen ...
|
|
|
command line
perl -MCPAN -e shell
cpan> install XML::Twig
|

May 26th, 2011, 08:55 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 5
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?
|

May 27th, 2011, 10:34 PM
|
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|