Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: XML Array/Hash Issue

by Preceptor (Deacon)
on Nov 20, 2013 at 21:37 UTC ( #1063615=note: print w/replies, xml ) Need Help??


in reply to XML Array/Hash Issue

Your problem is that you're getting a scalar in one mode, and an array in the other - and so your coercion is failing.

I would take a slightly different approach, and use XML::Twig, which is my current favorite XML parser.

use strict; use warnings; use XML::Twig; my $XML = "<errors> <error> <desc>First Error</desc> </error> <error> <desc>Second Error</desc> </error> </errors>"; sub print_error { my ( $twig, $error ) = @_; print "NEW: ",$error -> first_child_text('desc'),"\n"; } my $parser = XML::Twig -> new ( 'twig_handlers' => { 'error' => \&prin +t_error } ); $parser -> parse ( $XML );

You could also find the 'children' method within XML::Twig to be handy if you wanted to do it a different way.

sub process_all_errors { my ( $twig, $errors_list ) = @_; foreach my $error ( $errors_list -> children ( "error" ) ) { print "NEW: ",$error -> first_child_text('desc'),"\n"; } } my $other_parser = XML::Twig -> new ( 'twig_handlers' => { 'errors' => + \&process_all_errors } ); $other_parser -> parse ( $XML );

The former invokes the subroutine each time the parser hits an 'errors' element. (So twice). The latter invokes the sub just once (for each 'error') but then cycles through the 'errors' within it. (Which is optimal depends on what you're trying to do exactly, which is why I've given both examples).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063615]
help
Chatterbox?
[atcroft]: Lady_Aleena: I know that they used the L<text|url> syntax to link to them, but I was wondering if there was a more "standard" way (for instance, if the L<module_name> syntax might work better).
[Lady_Aleena]: atcroft, I"m trying to think of a module I've used that linked to another module.
[Lady_Aleena]: atcroft, I just put a link to another module in one of my local ones. For a normal link L<text|url> the text would be written out then the url would follow the text in brackets <>. However, when I linked to another module, the text was written ..

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2017-05-27 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?