public class XmlElement extends Object
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 |
---|---|
protected Map<String,Object> |
attributes
Attributes
|
static String |
HEADER
XML header
|
protected List<Object> |
children
Child elements
|
protected CharSequence |
name
Element name
|
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 XmlElement> |
addAttrib(CharSequence name,
Object value)
Add an attribute
|
<T extends XmlElement> |
addCDATA(CharSequence charData)
Add a character data in
CDATA format to XML only. |
<T extends XmlElement> |
addComment(CharSequence comment)
Add a comment text.
|
<T extends XmlElement> |
addElement(CharSequence name)
Create a new
XmlElement for a required name and add it to children. |
<T extends XmlElement> |
addElement(T element)
Add a child element
|
protected void |
addChild(Object child)
Add a child entity
|
<T extends XmlElement> |
addRawText(CharSequence rawText)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
<T extends XmlElement> |
addText(CharSequence text)
Add a text and escape special character
|
<T extends XmlElement> |
addTextWithSpace(CharSequence text)
Add a text including a space (before and after the text)
|
protected Map<String,Object> |
getAttribs()
Return attributes
|
Map<String,Object> |
getAttributes()
Get an unmodifiable map of attributes
|
List<Object> |
getChildren()
Get an unmodifiable list of children
|
String |
getName()
Get an element name
|
String |
toString()
Render the XML code including header
|
XmlWriter |
toWriter(int level,
XmlWriter out)
Render the XML code without header
|
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 XmlElement> T addElement(@Nonnull T element)
element
- Add a child element is required. An undefined argument is ignored.@Nonnull public final <T extends XmlElement> T addElement(@Nonnull CharSequence name)
XmlElement
for a required name and add it to children.name
- A name of the new XmlElement is requred.@Nonnull public final <T extends XmlElement> T addAttrib(@Nonnull CharSequence name, @Nullable Object value)
name
- Required element namevalue
- The null
value is ignored.@Nonnull public final <T extends XmlElement> T addText(@Nullable CharSequence text)
text
- text An empty argument is ignored.@Nonnull public final <T extends XmlElement> T addTextWithSpace(@Nullable CharSequence text)
text
- text An empty argument is ignored.@Nonnull public final <T extends XmlElement> T addRawText(@Nullable CharSequence rawText)
rawText
- text An empty argument is ignored.@Nonnull public final <T extends XmlElement> T addComment(@Nullable CharSequence comment)
comment
- A comment text must not contain a string -->
.@Nonnull public final <T extends XmlElement> T addCDATA(@Nullable CharSequence charData)
CDATA
format to XML only.
The CDATA structure isn't really for HTML at all.charData
- A text including the final DATA sequence. An empty argument is ignored.@Nonnull public Map<String,Object> getAttributes()
@Nonnull public XmlWriter toWriter(int level, @Nonnull XmlWriter out) throws IOException
IOException
Copyright 2019-2022, Pavel Ponec