public class XmlBuilder extends AbstractElement<XmlBuilder>
XmlPriter writer = XmlPriter.forXml();
try (XmlBuilder html = new XmlBuilder(Html.HTML, writer)) {
try (XmlBuilder head = html.addElement(Html.HEAD)) {
head.addElement(Html.META, Html.A_CHARSET, UTF_8);
head.addElement(Html.TITLE).addText("Test");
}
ry (XmlBuilder body = html.addElement(Html.BODY)) {
body.addElement(Html.H1).addText("Hello word!");
body.addElement(Html.DIV).addText(null);
}
};
String result = writer.toString();
The XmlElement class implements the Closeable implementation
for an optional highlighting the tree structure in the source code.HtmlElement| Modifier and Type | Field and Description |
|---|---|
protected static String |
REQUIRED_MSG
Assertion message template
|
DEFAULT_INTENDATION, HTML_DOCTYPE, name, XML_HEADER| Modifier | Constructor and Description |
|---|---|
|
XmlBuilder(CharSequence name,
XmlPrinter writer)
New element with a parent
|
|
XmlBuilder(CharSequence name,
XmlPrinter writer,
int level)
The new element constructor
|
protected |
XmlBuilder(CharSequence name,
XmlPrinter writer,
int level,
boolean printName)
The new element constructor
|
| Modifier and Type | Method and Description |
|---|---|
<T extends XmlBuilder> |
addCDATA(CharSequence charData)
Deprecated.
|
<T extends XmlBuilder> |
addComment(CharSequence comment)
Deprecated.
|
<T extends XmlBuilder> |
addElement(String name)
Create a new
XmlBuilder for a required name and add it to children. |
<T extends XmlBuilder> |
addRawText(Object value)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
<T extends XmlBuilder> |
addText(Object value)
Add a text and escape special character
|
void |
close()
Close the Node
|
int |
getLevel() |
XmlPrinter |
getWriter()
Writer
|
boolean |
isClosed()
Is the node closed?
|
boolean |
isFilled() |
boolean |
isLastText()
The last child was a text
|
protected XmlBuilder |
nextChild(XmlBuilder element)
Settup states
|
<T extends XmlBuilder> |
setAttrib(String name,
Object value)
Add an attribute
|
String |
toString()
Render the XML code including header
|
getNameprotected static final String REQUIRED_MSG
public XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer, int level) throws IOException
name - The element name must not be empty nor special HTML characters.IOExceptionprotected XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer, int level, boolean printName) throws IOException
name - The element name must not be empty nor special HTML characters.writer - A XmlPrinterlevel - Level of the ElementprintName - Print the element name immediately.IOExceptionpublic XmlBuilder(@Nonnull CharSequence name, @Nonnull XmlPrinter writer) throws IOException
IOException@Nonnull protected XmlBuilder nextChild(@Nullable XmlBuilder element) throws IOException
element - A child Node or null value for a text dataIOException@Nonnull public final <T extends XmlBuilder> T addElement(@Nonnull String name) throws IOException
XmlBuilder for a required name and add it to children.addElement in class AbstractElement<XmlBuilder>name - A name of the new XmlElement is required.IOException@Nonnull public final <T extends XmlBuilder> T setAttrib(@Nonnull String name, @Nullable Object value) throws IOException
setAttrib in class AbstractElement<XmlBuilder>name - Required element namevalue - The null value is ignored. Formatting is performed by the
XmlPrinter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString() only.IOException@Nonnull public final <T extends XmlBuilder> T addText(@Nullable Object value) throws IOException
addText in class AbstractElement<XmlBuilder>value - The null value is allowed. Formatting is performed by the
XmlPrinter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString() only.IOException@Nonnull public final <T extends XmlBuilder> T addRawText(@Nullable Object value) throws IOException
addRawText in class AbstractElement<XmlBuilder>value - The null value is ignored.IOException@Nonnull @Deprecated public final <T extends XmlBuilder> T addComment(@Nullable CharSequence comment)
addComment in class AbstractElement<XmlBuilder>comment - A comment text must not contain a string --> .@Nonnull @Deprecated public final <T extends XmlBuilder> T addCDATA(@Nullable CharSequence charData)
CDATA format to XML only.
The CDATA structure isn't really for HTML at all.addCDATA in class AbstractElement<XmlBuilder>charData - A text including the final DATA sequence. An empty argument is ignored.public final void close()
throws IOException
IOExceptionpublic boolean isClosed()
public int getLevel()
public boolean isFilled()
public boolean isLastText()
public XmlPrinter getWriter()
Copyright 2015, Pavel Ponec