Re: XML::Simple help

"Jenda Krynicky" <[email protected]>
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
From: "geek.shrek" <[email protected]>
> I'm trying to extract an element value by using XML Simple
> 
> Here's my input xml
> 
> 
> <Library>
>      <book>
>             <id>1</id>
>             <name>ABC</name>
>      </book>
>      <book>
>             <id>2</id>
>             <name>ABC123</name>
>      </book>
>      <book>
>             <id>3</id>
>             <name>ABCDEF</name>
>      </book>
> </Library>
> 
> I'm trying to get the name of id "1"

use XML::Rules;

my $parser = XML::Rules->new(
	stripspaces => 7,
	rules => {
		_default => 'content',
		book => 'by id',
		Library => 'pass',
	}
);

my $data = $parser->parsefile('library.xml');
#use Data::Dumper;
#print Dumper($data);

print $data->{1}{name};


or if you really wanted to remember just the ID and name:

use XML::Rules;

my $parser = XML::Rules->new(
	stripspaces => 7,
	rules => {
		_default => 'content',
		book => sub {$_[1]->{id} => $_[1]->{name}},
		Library => 'pass',
	}
);

my $data = $parser->parse(\*DATA);
#use Data::Dumper;
#print Dumper($data);

print $data->{1};


HTH, Jenda
P.S.: See http://www.perlmonks.org/index.pl?node_id=697036 please
===== [email protected] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
	-- Terry Pratchett in Sourcery

_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.