Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'm guessing that right now the code (you haven't posted any, so the best I can do is guess!) in the Char handler is saving a reference to the last bit of character data, and then when the End handler sees the end of the Title element, it does something with that. Maybe something like this:

use 5.010; use strict; use warnings; use XML::Parser; my ($got_title, $in_title); my $parser = XML::Parser->new( Handlers => { Start => sub { $in_title++ if $_[1] eq 'Title' }, End => sub { $in_title--, say "GOT TITLE: $got_title" if $_[ +1] eq 'Title' }, Char => sub { $got_title = $_[1] if $in_title }, }, ); $parser->parse(<<'XML'); <Document> <Title>Company A&amp;B Information</Title> <Abstract>Foo</Abstract> <Title>Company X&amp;Y Information</Title> <Abstract>Bar</Abstract> </Document> XML

Instead you want the Char handler to accumulate the pieces of character data using either string appending, or pushing onto an array/arrayref, then use the Start and End handlers to signal when to start and end accumulating character data. For example:

use 5.010; use strict; use warnings; use XML::Parser; my (@got_title, $in_title); my $parser = XML::Parser->new( Handlers => { Start => sub { $in_title++, @got_title = () if $_[1] eq 'Title +' }, End => sub { $in_title--, say "GOT TITLE: @got_title" if $_[ +1] eq 'Title'; }, Char => sub { push @got_title, $_[1] if $in_title }, }, ); $parser->parse(<<'XML'); <Document> <Title>Company A&amp;B Information</Title> <Abstract>Foo</Abstract> <Title>Company X&amp;Y Information</Title> <Abstract>Bar</Abstract> </Document> XML
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re^3: XML::Parser - Usage of &amp; by tobyink
in thread XML::Parser - Usage of &amp; by sumeetgrover

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found