public class XmlElement extends Object implements Element
XmlElement root = new XmlElement("root"); root.addElement("childA") .addAttrib("x", 1) .addAttrib("y", 2); root.addElement("childB") .addAttrib("x", 3) .addAttrib("y", 4) .addText("A text message <&\">"); root.addRawText("\n<rawXml/>\n"); root.addCDATA("A character data <&\">"); String result = root.toString();
HtmlElement
Modifier and Type | Class and Description |
---|---|
protected static class |
XmlElement.RawEnvelope
Raw XML code envelope
|
Modifier and Type | Field and Description |
---|---|
static String |
HEADER
XML header
|
protected CharSequence |
name
Element name
|
CDATA_BEG, CDATA_END, COMMENT_BEG, COMMENT_END, CHAR_NEW_LINE, CHAR_SPACE, XML_2QUOT, XML_AMP, XML_GT, XML_LT, XML_QUOT
Constructor and Description |
---|
XmlElement(CharSequence name)
The new element constructor
|
XmlElement(CharSequence name,
XmlElement parent)
New element with a parent
|
Modifier and Type | Method and Description |
---|---|
<T extends Element> |
addAttrib(CharSequence name,
Object value)
Add an attribute
|
<T extends Element> |
addCDATA(CharSequence charData)
Add a character data in
CDATA format to XML only. |
<T extends Element> |
addComment(CharSequence comment)
Add a comment text.
|
<T extends Element> |
addElement(CharSequence name)
Create a new
XmlElement for a required name and add it to children. |
<T extends Element> |
addElement(T element)
Add a child element
|
<T extends Element> |
addRawText(CharSequence rawText)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
<T extends Element> |
addText(CharSequence text)
Add a text and escape special character
|
<T extends Element> |
addTextWithSpace(CharSequence text)
Add a text including a space (before and after the text)
|
protected Map<String,Object> |
getAttribs()
Return attributes
|
protected List<Object> |
getChildren()
Return child entities
|
String |
toString()
Render the XML code including header
|
Writer |
toWriter(Writer out)
Render the XML code without header
|
protected void |
writeValue(Object value,
boolean attribute,
Writer out)
Write escaped value to the output
|
public static final String HEADER
@Nonnull protected final CharSequence name
public XmlElement(@Nonnull CharSequence name)
name
- The parameter must not be empty or contain any special HTML characters.public XmlElement(@Nonnull CharSequence name, @Nonnull XmlElement parent)
@Nonnull public final <T extends Element> T addElement(@Nonnull T element)
addElement
in interface Element
element
- Add a child element is required. An undefined argument is ignored.@Nonnull public final <T extends Element> T addElement(@Nonnull CharSequence name)
XmlElement
for a required name and add it to children.addElement
in interface Element
name
- A name of the new XmlElement is requred.@Nonnull public final <T extends Element> T addAttrib(@Nonnull CharSequence name, @Nullable Object value)
@Nonnull public final <T extends Element> T addText(@Nullable CharSequence text)
@Nonnull public final <T extends Element> T addTextWithSpace(@Nullable CharSequence text)
addTextWithSpace
in interface Element
text
- text An empty argument is ignored.@Nonnull public final <T extends Element> T addRawText(@Nullable CharSequence rawText)
addRawText
in interface Element
rawText
- text An empty argument is ignored.@Nonnull public final <T extends Element> T addComment(@Nullable CharSequence comment)
addComment
in interface Element
comment
- A comment text must not contain a string -->
.@Nonnull public final <T extends Element> T addCDATA(@Nullable CharSequence charData)
CDATA
format to XML only.
The CDATA structure isn't really for HTML at all.protected void writeValue(@Nonnull Object value, boolean attribute, @Nonnull Writer out) throws IOException
value
- A valueattribute
- Render the value to an element attribute, or a textout
- An output writerIOException
@Nonnull public Writer toWriter(@Nonnull Writer out) throws IOException
toWriter
in interface Element
IOException
Copyright 2019-2022, Pavel Ponec