Hi Monks, I need to parse a HTML file in my disk and add content to it in between UL tags and create another file and write out the output into it. But the thing is, I need to insert content in all UL tags except the first and last UL tag. I tried html::Parser and HTML::TokeParser, but some error in reading file occurs. Here is the file and code I used. Kindly help..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ +/www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="keywords" content="" /> <title>My Page</title> </head> <body> <div class="faq"> <ul id="faq"> <li> <a href="#quickglance" >At a quick glance </a></li> <li><a href="#page1">intro </a></li> </ul> <p class="border">&nbsp;</p> <h2><a name="quickglance">At a glance</a><br /> <br /> </h2> <ul> <li class="orangeTxt1"><a name="a1" id="a"><strong>How doe +s it work?</strong></a></li> <li><strong>Step 1:</strong> Commitment.</li> </ul> </p> <div class="border"> </div> <ul> <li class="orangeTxt1"><a name="a2" id="a2"><strong>Who ma +nages it?</strong></a></li> <li> owner actively commits by publicizing it to his/her + friends. </li> </ul> <p><strong>If you have other questions, please email us at:</strong> +</p> </div> <div class="footer"> <div class="footerLinks"> <ul> <li><a href="aboutus.html"> About us</a></li> <li> <a href="contact.html">Contact Us</a></li> </ul> </div> </div> </body> </html>

The Code I used for this is,

use strict; use warnings; our $content=''; our $val=''; our $count = 1; open (MYFILE, 'C:\Program Files\Perl Express\Scripts\faq.html'); print "The HTML File Contents are,\n"; my $mycount = 1; while ($content = <MYFILE>) { chop $content; $val = $content; my $ins = qq{ <span class="backTotop"> <a href="#a$count">Back to top</a></span> <div class="borderGoal"> </div> }; if ($val =~ m/<\/ul>/) { $count = $count + 1; } $val =~ s{(</ul>)}{$ins$1}gsi; if (-e "../faq1.html") { unlink ("../faq1.html"); open (DA, ">>faq1.html") or die "$! error trying to overwrite"; print DA "$val\n"; close (DA); } else { open (DA, ">>faq1.html") or die "$! error trying to overwrite"; print DA "$val\n"; close (DA); } }

In reply to need to parse a HTML file and add content to it by techtween

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.