Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by aardvark (Pilgrim)
on May 23, 2001 at 04:21 UTC ( [id://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
    aardvark,
    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,
    DrSax
      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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://82437]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-03-29 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found