Thursday, 2 October 2014

Perl XML::parser print single attribute value



I'm parsing a XML file and the file has nodes like this. <product id="12345" model="dvd" section="cmp" img="junk.jpg"></product>


Here is my code. I need to print the value of id attribute for all products.



use XML::Parser;
my $parser = XML::Parser->new( Handlers => { Start => \&handle_start } );
$parser->parsefile('D:\Project\mob.xml');

sub handle_start {
my ( $expat, $element, %attrs ) = @_;
if ( $element eq 'product' ) {
print $element;
}
}

No comments:

Post a Comment