Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: XML::Twig - can't find my uncle!

by aardvark (Pilgrim)
on May 23, 2001 at 04:21 UTC ( #82437=note: print w/replies, xml ) Need Help??

in reply to XML::Twig - can't find my uncle!

I hate unnecessary attributes. I try to use them only when it refines the meaning of the value of the element. If you create your xml with element values, I think you will have an easier time getting to them. Like this:
<log> <msg> <error> ID not found </error> <request> myRequest </request> </msg> </log>
But sometimes we just have to deal with the hand we are dealt. I found a way to get at your attribute values by registering two TwigHandlers. I'm sure mirod has a much more elegent solution than this:
use strict; use XML::Twig; my $file = 'uncle.xml'; my $twig = new XML::Twig( TwigHandlers => { 'log/msg/error' => \&print_error, 'log/msg/request' => \&print_request, }); $twig->parsefile($file) or die "can't parse $!\n"; sub print_error { my ($t, $elt) = @_; print "ERROR = " . $elt->att('name') . "\n"; } sub print_request { my ($t, $elt) = @_; print "REQUEST = " . $elt->att('name') . "\n"; }

Get Strong Together!!

Replies are listed 'Best First'.
Re: Re: XML::Twig - can't find my uncle!
by DrSax (Sexton) on May 23, 2001 at 07:15 UTC
    Thanks for looking in to this. As it turns out, I need to get the request only when there is an error and i need to co-relate these, so the code you are not seeing in my piddling example creates a nice structure to process, allowing requests to be repaired and re-submitted from a Web page.
    The print_request subroutine will fire for all requests, I believe, which leads to behavior that I don't want. I will see if mirod responds with something.
    Again, thanks for your time,
      ok, how about this?
      use strict; use XML::Twig; my $file = 'uncle.xml'; my $twig = new XML::Twig; $twig->parsefile($file); my $root = $twig->root; my @messages = $root->children; foreach my $msg (@messages) { my $error = $msg->first_child('error'); my $result = $msg->first_child('request'); my $error_name = $error->att('name'); my $result_name = $result->att('name'); if ($error_name && $result_name) { print "ERROR : $error_name \n"; print "RESULT : $result_name \n"; } }
      Get Strong Together!!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://82437]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2018-03-21 09:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (265 votes). Check out past polls.