I'm not sure what your ultimate goal is, but if you want to print out just the content of a "cdata" tag, with all other tags retained within that section of data, maybe something like this will do:
#!/usr/bin/perl use strict; use HTML::TokeParser; my $sample_HTML = <<EOD; <HTML> blah. <CDATA> Just some random whatever. It might have some <b>real</b> HTML like a +table or CSS styling or even some <H1>IMPORTANT</H1> words. Maybe even a form <form method= +post>...</form> </CDATA> </HTML> EOD my $p = HTML::TokeParser->new( \$sample_HTML ); my $in_cdata = 0; while ( my $token = $p->get_token ) { my ( $tkn_type, $tkn_content, @rest ) = @$token; if ( $tkn_type =~ /[SE]/ ) { $tkn_content = pop @rest; # last array element is full tag st +ring } print $tkn_content if ( $in_cdata and $tkn_content !~ /cdata/ ); if ( $tkn_content =~ /cdata/i ) { $in_cdata += ( $tkn_type eq 'S' ) ? 1 : -1; } }
That doesn't print the CDATA tags themselves, but it prints everything inside the CDATA tags, including other tags. To do that, the main loop has to process all "tokens" (all tags and all intervening text in the whole document) one token at a time, and a state variable has to keep track of when you're inside a cdata section as opposed to not being inside one.

In reply to Re: HTML::TokeParser Frustration by graff
in thread HTML::TokeParser Frustration by Anonymous Monk

Title:
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, details, 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, summary, 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.