By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

Parsing html chunks

P: 2
I have been trying to figure out the best way to parse some chunks of html code that contain tables. I've been trying to do this with HTML::Parser and HTML::TokeParser and HTML::TokeParser::Simple, but I just can't seem to get everything working the way I want. The chunks are from a standard template so there is some structure to the data, but it is not that simple.

The problem:
There can be bare text before, after or between each table in the chunk.
There can be embedded tables (tables within tables)
There can be multiple tables in series. (one after the other)
There can be embedded tables and tables in series in the same chunk.

If you've ever looked at a PHPBB forum, the chunks I am working with are the posts. The tables are quoted text within those posts. I need to remove the HTML code and replace it with the BBCode used by the forum so it can then be put into the database. I know PHPBB uses a database backend, but I do not and cannot get access to it.

What I am trying to accomplish is to pull the text out of the table and then eventually insert it into the database. Where I'm having problems is storing the text in a variable so that it can be recreated in the order in which it would look on the html page in a browser.

With HTML::Parser I was able to get a single table of data out, but when I started getting into the multiple levels of tables, I didn't really have a clean way to hold on to what I had already parsed while I continued through the chunk.

With HTML::TokeParser::Simple I was able to do the same thing, but I end up in a sea of flags trying to capture the right data at the right time. If I had a way with this module to extract the contents of the entire table structure and then parse that one thing at a time, I think I'd be all set. I seem to be able get close to the solution with both modules coming at the problem from two different directions, but can't quite get the entire solution.

Can anyone provide any insight into what I might possibly try or consider in order to get this project finished? I'm not asking for help with the coding per se, just a useful design method to solve this problem. I'm not a perl expert by any means, but I can make my way through the docs and some books to figure most things out.
Apr 2 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 2.5K+
P: 3,503
The HTML::Parser module is probably your best bet. Take a look at this tutorial and see if it helps.


Apr 5 '08 #2

Expert 2.5K+
P: 4,059
There is a module specifically for parsing html tables, HTML::TableParser, but I have never used it so can't help with the interface.
Apr 5 '08 #3

P: 2
Thanks for the pointers. I appreciate the help. The HTML::TableParser looks really good. I hadn't found that one. One thing I did do was to look back at my problem objectively and see why I couldn't do what I needed to. What I found was I was spending too much time looking at the output of the data and not the data itself. I think I found the most effective way for me to parse through the data and use a set of flags to capture the right data at the right time. Its working so I'll continue going with it. I did end up using HTML::TokeParser::Simple.

The problem I had with HTML::Parser was not being able to use a variable to store the text I wanted to save without creating a subclass. I'm not super knowledgeable on perl so I had a hard time figuring out how to do what I wanted with it. I use that for another piece of the same project and it worked perfect for that.

Thanks again for your suggestions.
Apr 6 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.