public class XmlBuilder extends Object implements ApiElement<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.| Modifier and Type | Field and Description |
|---|---|
static String |
HTML
The HTML tag name
|
protected @Nullable String |
name
Element name
|
protected static String |
REQUIRED_MSG
Assertion message template
|
HIDDEN_NAME| Modifier | Constructor and Description |
|---|---|
|
XmlBuilder(@NotNull String name,
@NotNull XmlPrinter writer)
New element with a parent
|
|
XmlBuilder(@Nullable String name,
@NotNull XmlPrinter writer,
int level)
The new element constructor
|
protected |
XmlBuilder(@Nullable String name,
@NotNull XmlPrinter writer,
int level,
boolean printName)
The new element constructor
|
| Modifier and Type | Method and Description |
|---|---|
@NotNull XmlBuilder |
addCDATA(@Nullable CharSequence charData)
Deprecated.
|
@NotNull XmlBuilder |
addComment(@Nullable CharSequence comment)
Deprecated.
|
@NotNull XmlBuilder |
addElement(@NotNull String name)
Create a new
XmlBuilder for a required name and add it to children. |
@NotNull XmlBuilder |
addRawText(@Nullable Object value)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
@NotNull XmlBuilder |
addText(@Nullable Object value)
Add a text and escape special character
|
@NotNull XmlBuilder |
addTextTemplated(@Nullable CharSequence template,
Object... values)
Message template with hight performance.
|
void |
close()
Close the Node
|
static @NotNull XmlBuilder |
forHtml(@NotNull Appendable response)
Create builder for HTML
|
static @NotNull XmlBuilder |
forNiceHtml(@NotNull Appendable response) |
int |
getLevel() |
@Nullable String |
getName()
Get an element name
|
XmlPrinter |
getWriter()
Writer
|
boolean |
isClosed()
Is the node closed?
|
boolean |
isFilled() |
boolean |
isLastText()
The last child was a text
|
protected @Nullable XmlBuilder |
nextChild(@Nullable XmlBuilder element)
Setup states
|
@NotNull XmlBuilder |
setAttribute(@Nullable String name,
@Nullable Object value)
Add an attribute
|
@NotNull String |
toString()
Render the XML code including header
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitsetAttribpublic static final String HTML
protected static final String REQUIRED_MSG
@Nullable protected final @Nullable String name
public XmlBuilder(@Nullable
@Nullable String name,
@NotNull
@NotNull XmlPrinter writer,
int level)
name - The element name must not be special HTML characters.
The null value is intended to build a root of AJAX queries.protected XmlBuilder(@Nullable
@Nullable String name,
@NotNull
@NotNull XmlPrinter writer,
int level,
boolean printName)
name - The element name must not be special HTML characters.
The null value is intended to build a root of AJAX queries.writer - A XmlPrinterlevel - Level of the ElementprintName - Print the element name immediately.public XmlBuilder(@NotNull
@NotNull String name,
@NotNull
@NotNull XmlPrinter writer)
@Nullable public @Nullable String getName()
ApiElementgetName in interface ApiElement<XmlBuilder>@Nullable protected @Nullable XmlBuilder nextChild(@Nullable @Nullable XmlBuilder element)
element - A child Node or null value for a text data@NotNull public final @NotNull XmlBuilder addElement(@NotNull @NotNull String name)
XmlBuilder for a required name and add it to children.addElement in interface ApiElement<XmlBuilder>name - A name of the new XmlElement is required.@NotNull public final @NotNull XmlBuilder setAttribute(@Nullable @Nullable String name, @Nullable @Nullable Object value)
setAttribute in interface ApiElement<XmlBuilder>name - Required element namevalue - The null value is ignored. Formatting is performed by the
AbstractWriter.writeValue(Object, ApiElement, String)
method, where the default implementation calls a toString() only.@NotNull public final @NotNull XmlBuilder addText(@Nullable @Nullable Object value)
addText in interface ApiElement<XmlBuilder>value - The null value is allowed. Formatting is performed by the
AbstractWriter.writeValue(Object, ApiElement, String)
method, where the default implementation calls a toString() only.@NotNull public final @NotNull XmlBuilder addTextTemplated(@Nullable @Nullable CharSequence template, @NotNull Object... values)
addTextTemplated in interface ApiElement<XmlBuilder>template - Message template where parameters are marked by the {} symbolvalues - argument values@NotNull public final @NotNull XmlBuilder addRawText(@Nullable @Nullable Object value)
addRawText in interface ApiElement<XmlBuilder>value - The null value is ignored.@NotNull @Deprecated public final @NotNull XmlBuilder addComment(@Nullable @Nullable CharSequence comment)
addComment in interface ApiElement<XmlBuilder>comment - A comment text must not contain a string --> .@NotNull @Deprecated public final @NotNull XmlBuilder addCDATA(@Nullable @Nullable CharSequence charData)
CDATA format to XML only.
The CDATA structure isn't really for HTML at all.addCDATA in interface ApiElement<XmlBuilder>charData - A text including the final DATA sequence. An empty argument is ignored.public final void close()
close in interface Closeableclose in interface AutoCloseableclose in interface ApiElement<XmlBuilder>public boolean isClosed()
public int getLevel()
public boolean isFilled()
public boolean isLastText()
public XmlPrinter getWriter()
@NotNull public @NotNull String toString()
@NotNull public static @NotNull XmlBuilder forHtml(@NotNull @NotNull Appendable response)
@NotNull public static @NotNull XmlBuilder forNiceHtml(@NotNull @NotNull Appendable response)
Copyright 2015, Pavel Ponec