Java libarary for HTML elements/tags
Anyone know a decent Java libarary for HTML elements?
Ex if I want to create an img-element, I would go new Image(...)
The object should support the normal HTML functions/attributes, such as setti开发者_开发百科ng CSS or disable.
Jakarta ECS sounds like what you are looking for.
You're not specifying exactly why you need this, but sounds like you want to programmatically create a web site, specifically you want to have it component based so that each class encapsulates the function of corresponding tag.
There's two ways to go, one is premade component based web frameworks which is the preferred way since those things already have a lot of things thought out and implemented for you. For example Apache Wicket, Apache Tapestry and Google Web Toolkit are all component based just to name a few.
The other way you can approach this is to roll your own. In this case you most likely will lose a lot of the flexibility provided by the aforementioned frameworks but then again, you have a piece of code which will do exactly what you want. So, for rolling out your own for exactly the purpose you mentioned I'd do the following:
- Take any DOM manipulation library such as JDOM, DOM4J, XOM etc. You can of course use Java's native one too, but it's mostly a huge pain in the ass to use extensively.
- Write factory methods/wrapper classes for each individual tag, such as image. For example this could be the Image class content using JDOM:
.
public interface MyElement<T extends org.jdom.Content> {
T getElement();
}
public class Image implements MyElement<org.jdom.Element> {
private final String source;
public Image(String source) {
this.source = source;
}
public org.jdom.Element getElement() {
return new Element("img").setAttribute("src", source);
}
}
and in some component class describing the whole page you would then add and instance of this class to a list/tree structure and then just finally call the getElement() to get tag specific markup wherever applicable. Note that this is a lot harder than it sounds, mostly because of the required nested structure of XML.
Try cobra.
Cobra is a pure Java HTML renderer and DOM parser that is being developed to support HTML 4, Javascript and CSS 2.
Cobra can be used as a Javascript-aware and CSS-aware HTML parser, independently of the Cobra rendering engine. Javascript DOM modifications that occur during parsing (e.g. via document.write) will be reflected in the parsed DOM, unless Javascript is disabled.
精彩评论