Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Question about XML::Simple

by euz (Initiate)
on Mar 11, 2012 at 13:14 UTC ( [id://958943]=perlquestion: print w/replies, xml ) Need Help??

euz has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Here s my problem (which I can't solve since a long long tiiiime), I can live with it but I was wondering if it could be solved :)

1/ I have a perl script reading a XML file :
<conf name="snoopdogg" serial="1" target="172.16.1.3"> <services> <service name="cpu"> <plugins> <plugin name="cpu" target="172.16.1.3" schedule="60"> <probes> <probe name="load1"> <warn value="4" operator="&gt;"/> <crit value="10" operator="&gt;"/> </probe> (...)
2/ I read it this way :
my $configuration = $xml->XMLin( $conf, GroupTags => { services => 'service', plugins => 'plugin', probes => 'probe' }, ForceArray => [ qw(service plugin probe) ], KeyAttr => { 'service' => 'name', 'plugin' => 'name', 'probe' => 'name' }, KeepRoot => 0 );
The problem is the following : I want to access a probe's warn value, to do so I have to :
my $warn = $configuration->{snoopdogg}{services}{cpu}{plugins}{cpu}{pr +obes}{load1}{warn}{value};
What I would like is to access the value this way :
my $warn = $configuration->{snoopdogg}{cpu}{cpu}{load1}{warn}{value};
Is there a way with XML::Simple to achieve something like
GroupTags => { services => 'service' => name }
Thank you very much ! :)

Replies are listed 'Best First'.
Re: Question about XML::Simple
by Jenda (Abbot) on Mar 11, 2012 at 15:15 UTC

    I don't think XML::Simple gets you that far. XML::Rules would.

    use XML::Rules; my $parser = XML::Rules->new( stripspaces => 7, rules => { 'services,plugins,probes' => 'pass', 'conf,service,plugin,probe' => 'by name', _default => 'as is' } ); my $configuration = $parser->parse(\*DATA); use Data::Dumper; print Dumper($configuration); __DATA__ <conf name="snoopdogg" serial="1" target="172.16.1.3"> <services> <service name="cpu"> <plugins> <plugin name="cpu" target="172.16.1.3" schedule="60"> <probes> <probe name="load1"> <warn value="4" operator="&gt;"/> <crit value="10" operator="&gt;"/> </probe> </probes> </plugin></plugins></service></services> </conf>

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Re: Question about XML::Simple
by kcott (Archbishop) on Mar 11, 2012 at 15:02 UTC
Re: Question about XML::Simple
by euz (Initiate) on Mar 11, 2012 at 16:23 UTC
    Thank you for your answers, XML::Rules seems promising.

    I will give it a try :)

    Have a nice day !

Re: Question about XML::Simple
by repellent (Priest) on Mar 11, 2012 at 21:37 UTC
    useXML::Twig;

    # we want anxpath like: //[@name="a"]//...//[@name="b"]/warn
    my @names = qw(snoopdogg cpu cpu load1); my $twig = XML::Twig->new; $twig->parse(<<'XML'); <conf name="snoopdogg" serial="1" target="172.16.1.3"> <services> <service name="cpu"> <plugins> <plugin name="cpu" target="172.16.1.3" schedule="60"> <probes> <probe name="load1"> <warn value="4" operator="&gt;"/> <crit value="10" operator="&gt;"/> </probe> </probes> </plugin></plugins></service></services> </conf> XML my ($elt) = $twig->get_xpath( (join "//" => "", map qq{[\@name="$_"]}, @names) . "/warn" ); printf "value: %d\n", $elt->att('value'); __END__ value: 4

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://958943]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-24 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found