public class XmlElement extends AbstractElement<XmlElement> implements Serializable
XmlElement root = new XmlElement("root");
root.addElement("childA")
.setAttrib("x", 1)
.setAttrib("y", 2);
root.addElement("childB")
.setAttrib("x", 3)
.setAttrib("y", 4)
.addText("A text message <&\">");
root.addRawText("\n<rawXml/>\n");
root.addCDATA("A character data <&\">");
String result = root.toString();
The XmlElement class implements the Closeable implementation
for an optional highlighting the tree structure in the source code.HtmlElement,
Serialized Form| 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
|
protected List<Object> |
children
Child elements with a
null items |
DEFAULT_INTENDATION, HTML_DOCTYPE, name, REQUIRED_MSG, XML_HEADER| 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(String name,
Object value)
Deprecated.
Uset the method
setAttrib(java.lang.String, java.lang.Object) instead of |
<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(String 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(Object value)
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles
|
<T extends XmlElement> |
addText(Object value)
Add a text and escape special character
|
void |
close()
An empty method
|
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
|
<T extends XmlElement> |
setAttrib(String name,
Object value)
Set one attribute
|
String |
toString()
Render the XML code including header
|
XmlWriter |
toWriter(int level,
XmlWriter out)
Render the XML code without header
|
getNamepublic XmlElement(@Nonnull CharSequence name)
name - The element name must not be empty nor 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 <T extends XmlElement> T addElement(@Nonnull String name)
XmlElement for a required name and add it to children.addElement in class AbstractElement<XmlElement>name - A name of the new XmlElement is required.@Nonnull @Deprecated public final <T extends XmlElement> T addAttrib(@Nonnull String name, @Nullable Object value)
setAttrib(java.lang.String, java.lang.Object) instead of@Nonnull public final <T extends XmlElement> T setAttrib(@Nonnull String name, @Nullable Object value)
setAttrib in class AbstractElement<XmlElement>name - Required element namevalue - The null value is ignored. Formatting is performed by the
XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString() only.@Nonnull public final <T extends XmlElement> T addText(@Nullable Object value)
addText in class AbstractElement<XmlElement>value - The null value is allowed. Formatting is performed by the
XmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)
method, where the default implementation calls a toString() only.@Nonnull public final <T extends XmlElement> T addRawText(@Nullable Object value)
addRawText in class AbstractElement<XmlElement>value - The null value is ignored.@Nonnull public final <T extends XmlElement> T addComment(@Nullable CharSequence comment)
addComment in class AbstractElement<XmlElement>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.addCDATA in class AbstractElement<XmlElement>charData - A text including the final DATA sequence. An empty argument is ignored.@Nonnull public Map<String,Object> getAttributes()
public final void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOException@Nonnull public XmlWriter toWriter(int level, @Nonnull XmlWriter out) throws IOException
IOExceptionCopyright 2015, Pavel Ponec