A good HTML object model in Java?

拥有回忆 提交于 2019-12-01 21:45:15

问题


I'm looking for an HTML object model in Java, capable of parsing HTML (not required) and containing all HTML elements (and CSS as well) in an elegant object model.

I'm looking for a pure java version of the Groovy's HTML builder. (I have no luck on google with this request.)

I want to be able to perform stuff like:

HTML html = new HTML();
Body body = html.body();

Table table body.addTable(myCssStyle);
Row row = table.addRow("a", "b", "c").withCss(cssRowStyle);

and so on...


回答1:


Jakarta ECS might be able to do what you want.




回答2:


Check out Jsoup:

Example: (Building some html)

Document doc = Document.createShell("");

Element headline = doc.body().appendElement("h1").text("thats a headline");
Element pTag = doc.body().appendElement("p").text("some text ...");
Element span = pTag.prependElement("span").text("That's");

System.out.println(doc);

Output:

<html>
 <head></head>
 <body>
  <h1>thats a headline</h1>
  <p><span>That's</span>some text ...</p>
 </body>
</html>

Documentation:

  • Codebook
  • API Documentation (JavaDoc)



回答3:


Just an idea: you could take a look at the source code of xhtmlrenderer project. http://code.google.com/p/flying-saucer//

It's not plain HTML (it's XHTML), but may be a good starting point, don't you think?



来源:https://stackoverflow.com/questions/2119180/a-good-html-object-model-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!