Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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).

In reply to Re: XML Array/Hash Issue by Preceptor
in thread XML Array/Hash Issue by nickt9999

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [LanX]: Plenty of "templates" realised with CGI-HTML-function
    [LanX]: well if you want you can fetch all XML and translate it to another board markup
    [moritz]: at this time, a migration could consist of only migrating the rendered HTML of nodes
    [moritz]: to a forum software that supports raw HTML input for legacy threads, and uses sane markup for everything else
    [holli]: yeah, then vroom will personally stab me in the back at night
    [holli]: exactly, moritz
    [moritz]: with a big table of redirects to support the old links
    [LanX]: do it
    [holli]: well if you put some cleverness into the migration you can auto convert most of the links as well
    [LanX]: Let's Make PerlMonks Great Again!

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (8)
    As of 2017-11-20 19:33 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (292 votes). Check out past polls.