#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep 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>
  2. #2
  3. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    code tried till now.


    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 );
    }
    }
    }
    }
    }
    }
  6. #4
  7. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    Try this ;

    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";
    						$value =~ s/,/./g;
    						print LOGFILE "Value : $value \n";
    						push( @ecm_amt_arr, $value );
    					} else {
    						if ($key =~ /INVOICEDATE/) {
    							$inv_date=$value;
    						}
    					}
    				}
    			}
    		}
        }
    }
    --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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    180
    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>
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep 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;
    }
    }
    }
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    180
    That's exactly what i just demonstrated.

    - M
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0
    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....
  16. #9
  17. 'fie' on me, allege-dly
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2003
    Location
    in da kitchen ...
    Posts
    12,889
    Rep Power
    6444
    command line
    perl -MCPAN -e shell
    cpan> install XML::Twig
    --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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    5
    Rep Power
    0
    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?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    104
    Rep Power
    180
    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

IMN logo majestic logo threadwatch logo seochat tools logo