Package com.oorian.xml
Class XmlElement
java.lang.Object
com.oorian.xml.XmlElement
- All Implemented Interfaces:
XmlNode
Represents an XML element with a name, attributes, and child nodes.
XmlElement is the primary class for building and manipulating XML documents programmatically. It provides a fluent API for constructing XML trees and supports elements, text content, CDATA sections, and attributes.
- Since:
- 2020
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionXmlElement(String name) Constructs a new XmlElement with the specified tag name. -
Method Summary
Modifier and TypeMethodDescriptionaddAttribute(String key, String value) Adds or replaces an attribute on this element.Adds a CDATA section as a child of this element.addElement(XmlNode child) Adds a child node to this element.addElements(List<? extends XmlNode> children) Adds multiple child nodes to this element.booleancontainsElement(XmlNode child) Checks if this element contains the specified child node.getAttribute(String name) Returns the value of the specified attribute, or null if not found.Returns the attribute map of this element.getElementById(String id) Returns the first descendant element with the specified id attribute, or null if not found.getElementByTagName(String tagName) Returns the first direct child element with the specified tag name, or null if not found.getElementByTagName(String tagName, boolean recursive) Returns the first child element with the specified tag name, or null if not found.Returns the child nodes of this element.getElementsByTagName(String tagName) Returns all direct child elements with the specified tag name.getElementsByTagName(String tagName, boolean recursive) Returns all child elements with the specified tag name.Returns the next sibling element, or null if this is the last child or has no parent.Returns the parent element, or null if this is the root element.Returns the previous sibling element, or null if this is the first child or has no parent.Returns the tag name of this element.getText()Returns the combined text content of all TextNode children.booleanhasAttribute(String name) Checks if the element has the specified attribute.booleanChecks if this element has any child nodes.Removes all child nodes.removeAttribute(String name) Removes the specified attribute.booleanremoveElement(XmlNode child) Removes the specified child node.setTagName(String name) Sets the element tag name.Sets the text content of this element, replacing all existing children.toString()toXmlString(int indent) Returns the XML string representation of this node with the specified indentation level.
-
Constructor Details
-
XmlElement
Constructs a new XmlElement with the specified tag name.- Parameters:
name- the element tag name.
-
-
Method Details
-
addAttribute
Adds or replaces an attribute on this element.- Parameters:
key- the attribute name.value- the attribute value.- Returns:
- this element for method chaining.
-
setText
Sets the text content of this element, replacing all existing children.- Parameters:
text- the text content.- Returns:
- this element for method chaining.
-
addElement
Adds a child node to this element.If the child is an XmlElement, its parent is set to this element.
- Parameters:
child- the child node to add.- Returns:
- this element for method chaining.
-
addElements
Adds multiple child nodes to this element.- Parameters:
children- the child nodes to add.- Returns:
- this element for method chaining.
-
addCData
Adds a CDATA section as a child of this element.- Parameters:
cdata- the CDATA content.- Returns:
- this element for method chaining.
-
getTagName
Returns the tag name of this element.- Returns:
- the tag name.
-
getAttributes
Returns the attribute map of this element.- Returns:
- the attributes as a map of name-value pairs.
-
getElements
Returns the child nodes of this element.- Returns:
- the list of child nodes.
-
getParent
Returns the parent element, or null if this is the root element.- Returns:
- the parent element, or null.
-
getElementsByTagName
Returns all direct child elements with the specified tag name.- Parameters:
tagName- The element tag name to search for- Returns:
- A list of matching child elements
-
getElementsByTagName
Returns all child elements with the specified tag name.If
recursiveistrue, searches all descendants; iffalse, only direct children.- Parameters:
tagName- The element tag name to search forrecursive- Iftrue, searches all descendants- Returns:
- A list of matching elements
-
getElementByTagName
Returns the first direct child element with the specified tag name, or null if not found.- Parameters:
tagName- The element tag name to search for- Returns:
- The first matching child element, or null
-
getElementByTagName
Returns the first child element with the specified tag name, or null if not found.If
recursiveistrue, searches direct children first, then recurses into descendants. Iffalse, only searches direct children.- Parameters:
tagName- The element tag name to search forrecursive- Iftrue, searches all descendants- Returns:
- The first matching element, or null
-
getAttribute
Returns the value of the specified attribute, or null if not found.- Parameters:
name- The attribute name- Returns:
- The attribute value, or null
-
hasAttribute
Checks if the element has the specified attribute.- Parameters:
name- The attribute name- Returns:
- true if the attribute exists
-
removeAttribute
Removes the specified attribute.- Parameters:
name- The attribute name to remove- Returns:
- This element for method chaining
-
getText
Returns the combined text content of all TextNode children. For elements with mixed content (text and child elements), only the text portions are returned.- Returns:
- The text content, or empty string if no text nodes
-
removeElement
Removes the specified child node.- Parameters:
child- The child node to remove- Returns:
- true if the child was found and removed
-
removeAllElements
Removes all child nodes.- Returns:
- This element for method chaining
-
setTagName
Sets the element tag name.- Parameters:
name- The new element tag name- Returns:
- This element for method chaining
-
getElementById
Returns the first descendant element with the specified id attribute, or null if not found.- Parameters:
id- The id attribute value to search for- Returns:
- The matching element, or null
-
containsElement
Checks if this element contains the specified child node.- Parameters:
child- The child node to check for- Returns:
- true if the child is a direct child of this element
-
hasElements
public boolean hasElements()Checks if this element has any child nodes.- Returns:
- true if this element has one or more children
-
getNextSibling
Returns the next sibling element, or null if this is the last child or has no parent.- Returns:
- The next sibling element, or null
-
getPrevSibling
Returns the previous sibling element, or null if this is the first child or has no parent.- Returns:
- The previous sibling element, or null
-
toXmlString
Description copied from interface:XmlNodeReturns the XML string representation of this node with the specified indentation level.- Specified by:
toXmlStringin interfaceXmlNode- Parameters:
indent- the indentation level (number of two-space indents).- Returns:
- the formatted XML string.
-
toString
-