public class HtmlElement extends HtmlEmptyElement
HtmlTable
class to create tables.
HtmlElement root = new HtmlElement();
root.findElement("head").addChild(new HtmlElement("title", "The title"));
root.findElement("body").addChild(new HtmlElement("p", "The paragraph's text"));
System.out.println(root.toHtml());
Example 2: Create an HTML page with a simple form that has some attributes.
HtmlElement root = new HtmlElement();
root.findElement("body").addChild(new HtmlElement("form",
new HtmlAttribute("id", "loginForm"), new HtmlAttribute("onclick", "alert(\"hello\"")));
System.out.println(root.toHtml());
Constructor and Description |
---|
HtmlElement()
Constructs the root "html" element with nested "head" and "body" tags.
|
HtmlElement(String name)
Constructs this HTML element from the supplied (element) name.
|
HtmlElement(String name,
HtmlAttribute... attributes)
Constructs this HTML element from the supplied (element) name and attributes.
|
HtmlElement(String name,
String content)
Constructs this HTML element from the supplied (element) name and content.
|
HtmlElement(String name,
String content,
HtmlAttribute... attributes)
Constructs this HTML element from the supplied (element) name, content and attributes.
|
Modifier and Type | Method and Description |
---|---|
HtmlElement |
addChild(HtmlElement element)
Adds the supplied element as a child element of this HTML element.
|
HtmlElement |
addContent(String content)
Adds the supplied content to the content of this HTML element.
|
HtmlElement |
findElement(String name)
Finds the HTML element with the supplied name (case insensitive).
|
String |
getContent()
Gets the content of this HtmlElement.
|
String |
toFormattedHtml()
Creates a formatted HTML representation of this HTML element, including its attributes, content and children.
|
String |
toHtml()
Creates an HTML representation of this HTML element, including its attributes, content and children.
|
getName, toString
public HtmlElement()
public HtmlElement(String name)
name
- Name of this HTML element.NullPointerException
- When the supplied name is null.IllegalArgumentException
- When the supplied name is empty.public HtmlElement(String name, String content)
name
- Name of this HTML element.content
- The content of this HTML element.NullPointerException
- When the supplied name is null.IllegalArgumentException
- When the supplied name is empty.public HtmlElement(String name, HtmlAttribute... attributes)
name
- Name of this HTML element.attributes
- The optional attributes of this HTML element.NullPointerException
- When the supplied name is null.IllegalArgumentException
- When the supplied name is empty.public HtmlElement(String name, String content, HtmlAttribute... attributes)
name
- Name of this HTML element.content
- The content of this HTML element.attributes
- The optional attributes of this HTML element.NullPointerException
- When the supplied name is null.IllegalArgumentException
- When the supplied name is empty.public HtmlElement addContent(String content)
content
- The content to add to the content of this HTML element.NullPointerException
- When the supplied content is null.public String getContent()
public HtmlElement addChild(HtmlElement element)
element
- The element to add as a child of this HTML element.NullPointerException
- When the supplied element is null.public HtmlElement findElement(String name)
name
- The name of the HTML element to find.public String toHtml()
toHtml
in class HtmlEmptyElement
public String toFormattedHtml()
Copyright © 2008–2018. All rights reserved.