views:

64

answers:

4

Hello everyone,

I am doing a project wherein I need to read a HTML file and identify specific tags, modify the contents of the tag and create a new HTML file. Is there a library that parses HTML tags and is capable of writing the tags back to a new file ?

Cheers !!! Chaitannya

A: 

Look at http://java-source.net/open-source/html-parsers for a list of java libraries that parse html files into java objects that can be manipulated.

If the html files you are working with are well formed (xhtml) then you can also use XML libraries in java to find particular tags and modify them. The IO itself should be handled by the particular libraries you are using.

If you choose to manually parse the strings you could use regular expressions to find particular tags and use the java io libraries to write to the files and create new html documents. But this method reinvents the wheel so to speak because you have to manage tag opening and closing and all of those things are handled by pre-existing libraries.

controlfreak123
+1  A: 

Hi,

There are too many HTML parsers. You could use JTidy (http://jtidy.sourceforge.net/ ), NekoHTML (http://nekohtml.sourceforge.net/) or check TagSoup.

I usually prefer parsing XHTML with the standard Java XML Parsers, but you can't do this for any type of HTML.

ivy
I used both JTidy and Nekohtml - both are pretty good.
Slava Imeshev
+1  A: 

Hi,
if you want to modify web page and return modified content, I thnk the best way is to use XSL transformation.
http://en.wikipedia.org/wiki/XSLT

Konoplianko
I guess this was what I was looking forward to. I need to generate reports and present it in the format the user would like it to be presented.
chai
I am using Xalan processor currently and the code is working beautifully. Here is the link for the processor http://xml.apache.org/xalan-j/
chai
+2  A: 

Check out http://jsoup.org, it has a friendly dom-like API, for simple tasks you don't need to parse the html.

Victor Ionescu