Search code examples
javahtmldomhtmlcleaner

What library to use for building HTML documents?


Could please anybody recommend libraries that are able to do the opposite thing than these libraries ?

HtmlCleaner, TagSoup, HtmlParser, HtmlUnit, jSoup, jTidy, nekoHtml, WebHarvest or Jericho.

I need to build html pages, build the DOM model from String content.

EDIT: I need it for testing purposes. I have various types of input/strings that might be in the html page on various places... So I need to dynamically build it up... I then process the html page based on various criterions that must be fulfilled or not.

I will show you why I asked this question, consider htmlCleaner for this job :

List<String> paragraphs = getParagraphs(entity.getFile());
List<TagNode> pNodes = new ArrayList<TagNode>();

TagNode html = cleaner.clean("<html/>");
for(String paragraph : paragraphs) {                
    TagNode p = new TagNode("p");
    pNodes.add(p);
    // CANNOT setText() ?
}
html.addChildren(pNodes);

The problem is that TagNode has getText() method, but no setText() method ....

Please add more comments about how vague this question is ... The best thing you can do


Solution

  • If you are interested in HtmlCleaner particularly, it is actually a very convenient choice for building html documents.

    But you must know that if you want to set content to a TagNode, you append a child ContentNode element :-)

    List<String> paragraphs = getParagraphs(entity.getFile());
    List<TagNode> pNodes = new ArrayList<TagNode>();
    
    TagNode html = new TagNode("html");
    for(String paragraph : paragraphs) {                
        TagNode p = new TagNode("p");
        p.addChild(new ContentNode(paragraph));
        pNodes.add(p);
    }
    html.addChildren(pNodes);