开发者

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 setT开发者_StackOverflow社区ext() ?
}
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


Jsoup, Jsoup, Jsoup! I've used all of those, and it's my favorite by a long shot. You can use it to build documents, plus it brings a lot of the magic of Jquery-style traversing alongside the best HTML document parsing I've seen to date in a Java library. I'm so happy with it that I don't mind shamelessly promoting it. ;)


There are lot of template libraries for Java, from JSP to FreeMarker, from specific implementations in various frameworks (Spring?) to generic libraries like StringTemplate.

The most difficult task is... to make a choice.

In general, these libraries offer to make a skeleton of Web page, with "holes" to fill with variables. It is the simplest approach, often working well with tools.
If you really want to build from Dom, you can just use an XML library and generate XHTML.


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);


jwebutils -- A library for creating HTML 5 markup using Java. It also contains support for creating JSON and CSS 3 markup.

Jakarta Element Construction Set (ECS) - A Java API for generating elements for various markup languages it directly supports HTML 4.0 and XML. Now retired, but some folks really like it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜