Package org.ujorm.tools.xml.model
Class XmlModel
java.lang.Object
org.ujorm.tools.xml.model.XmlModel
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable,ApiElement<XmlModel>
- Direct Known Subclasses:
XmlElement
XML element model to rendering a XML file.
The main benefits are:
- secure building well-formed XML documents by the Java code
- a simple API built on a single XmlElement class
- creating XML components by a subclass is possible
- great performance and small memory footprint
How to use the class:
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();
- Since:
- 2.03
- Author:
- Pavel Ponec
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classRaw XML code envelope -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal @NotNull XmlModeladdCDATA(@Nullable CharSequence charData) Add a character data inCDATAformat to XML only.final @NotNull XmlModeladdComment(@Nullable CharSequence comment) Add a comment text.@NotNull XmlModeladdElement(@NotNull String name) Create a newXmlModelfor a required name and add it to children.final @NotNull XmlModeladdElement(@NotNull XmlModel element) Add a child elementprotected voidAdd a child entityfinal @NotNull XmlModeladdRawText(@Nullable Object value) Add an native text with no escaped characters, for example: XML code, JavaScript, CSS stylesfinal @NotNull XmlModelAdd a text and escape special characterfinal @NotNull XmlModeladdTextTemplated(@Nullable CharSequence template, @NotNull Object... values) Message template with hight performance.final voidclose()An empty methodReturn attributesGet an unmodifiable map of attributesGet an unmodifiable list of children@Nullable StringgetName()Get an element namefinal @NotNull XmlModelsetAttribute(@NotNull String name, @Nullable Object value) Set one attribute@NotNull StringtoString()Render the XML code including header@NotNull XmlWriterRender the XML code without headerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.ujorm.tools.xml.ApiElement
setAttrib
-
Field Details
-
name
Element name -
attributes
Attributes -
children
Child elements with anullitems
-
-
Constructor Details
-
XmlModel
- Parameters:
name- The element name must not be special HTML characters. Thenullvalue is intended to build a root of AJAX queries.
-
XmlModel
New element with a parent
-
-
Method Details
-
getName
Description copied from interface:ApiElementGet an element name- Specified by:
getNamein interfaceApiElement<XmlModel>
-
getAttribs
Return attributes -
addChild
Add a child entity -
addElement
Add a child element- Parameters:
element- Add a child element is required. An undefined argument is ignored.- Returns:
- The argument type of XmlElement!
-
addElement
Create a newXmlModelfor a required name and add it to children.- Specified by:
addElementin interfaceApiElement<XmlModel>- Parameters:
name- A name of the new XmlElement is required.- Returns:
- The new XmlElement!
-
setAttribute
@NotNull public final @NotNull XmlModel setAttribute(@NotNull @NotNull String name, @Nullable @Nullable Object value) Set one attribute- Specified by:
setAttributein interfaceApiElement<XmlModel>- Parameters:
name- Required element namevalue- Thenullvalue is ignored. Formatting is performed by theXmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)method, where the default implementation calls atoString()only.- Returns:
- The original element
-
addText
Add a text and escape special character- Specified by:
addTextin interfaceApiElement<XmlModel>- Parameters:
value- Thenullvalue is allowed. Formatting is performed by theXmlWriter#writeValue(java.lang.Object, org.ujorm.tools.dom.XmlElement, java.lang.String, java.io.Writer)method, where the default implementation calls atoString()only.- Returns:
- This instance
-
addTextTemplated
@NotNull public final @NotNull XmlModel addTextTemplated(@Nullable @Nullable CharSequence template, @NotNull @NotNull Object... values) Message template with hight performance.- Specified by:
addTextTemplatedin interfaceApiElement<XmlModel>- Parameters:
template- Message template where parameters are marked by the{}symbolvalues- argument values- Returns:
- The original builder
-
addRawText
Add an native text with no escaped characters, for example: XML code, JavaScript, CSS styles- Specified by:
addRawTextin interfaceApiElement<XmlModel>- Parameters:
value- Thenullvalue is ignored.- Returns:
- This instance
-
addComment
Add a comment text. The CDATA structure isn't really for HTML at all.- Specified by:
addCommentin interfaceApiElement<XmlModel>- Parameters:
comment- A comment text must not contain a string-->.- Returns:
- This instance
-
addCDATA
Add a character data inCDATAformat to XML only. The CDATA structure isn't really for HTML at all.- Specified by:
addCDATAin interfaceApiElement<XmlModel>- Parameters:
charData- A text including the final DATA sequence. An empty argument is ignored.- Returns:
- This instance
-
getAttributes
Get an unmodifiable map of attributes -
getChildren
Get an unmodifiable list of children -
close
public final void close()An empty method- Specified by:
closein interfaceApiElement<XmlModel>- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
Render the XML code including header -
toWriter
@NotNull public @NotNull XmlWriter toWriter(int level, @NotNull @NotNull XmlWriter out) throws IOException Render the XML code without header- Throws:
IOException
-