Class XmlElement

java.lang.Object
com.oorian.xml.XmlElement
All Implemented Interfaces:
XmlNode

public class XmlElement extends Object implements 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 Details

    • XmlElement

      public XmlElement(String name)
      Constructs a new XmlElement with the specified tag name.
      Parameters:
      name - the element tag name.
  • Method Details

    • addAttribute

      public XmlElement addAttribute(String key, String value)
      Adds or replaces an attribute on this element.
      Parameters:
      key - the attribute name.
      value - the attribute value.
      Returns:
      this element for method chaining.
    • setText

      public XmlElement setText(String text)
      Sets the text content of this element, replacing all existing children.
      Parameters:
      text - the text content.
      Returns:
      this element for method chaining.
    • addElement

      public XmlElement addElement(XmlNode child)
      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

      public XmlElement addElements(List<? extends XmlNode> children)
      Adds multiple child nodes to this element.
      Parameters:
      children - the child nodes to add.
      Returns:
      this element for method chaining.
    • addCData

      public XmlElement addCData(String cdata)
      Adds a CDATA section as a child of this element.
      Parameters:
      cdata - the CDATA content.
      Returns:
      this element for method chaining.
    • getTagName

      public String getTagName()
      Returns the tag name of this element.
      Returns:
      the tag name.
    • getAttributes

      public Map<String,String> getAttributes()
      Returns the attribute map of this element.
      Returns:
      the attributes as a map of name-value pairs.
    • getElements

      public List<XmlNode> getElements()
      Returns the child nodes of this element.
      Returns:
      the list of child nodes.
    • getParent

      public XmlElement getParent()
      Returns the parent element, or null if this is the root element.
      Returns:
      the parent element, or null.
    • getElementsByTagName

      public List<XmlElement> getElementsByTagName(String tagName)
      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

      public List<XmlElement> getElementsByTagName(String tagName, boolean recursive)
      Returns all child elements with the specified tag name.

      If recursive is true, searches all descendants; if false, only direct children.

      Parameters:
      tagName - The element tag name to search for
      recursive - If true, searches all descendants
      Returns:
      A list of matching elements
    • getElementByTagName

      public XmlElement getElementByTagName(String tagName)
      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

      public XmlElement getElementByTagName(String tagName, boolean recursive)
      Returns the first child element with the specified tag name, or null if not found.

      If recursive is true, searches direct children first, then recurses into descendants. If false, only searches direct children.

      Parameters:
      tagName - The element tag name to search for
      recursive - If true, searches all descendants
      Returns:
      The first matching element, or null
    • getAttribute

      public String getAttribute(String name)
      Returns the value of the specified attribute, or null if not found.
      Parameters:
      name - The attribute name
      Returns:
      The attribute value, or null
    • hasAttribute

      public boolean hasAttribute(String name)
      Checks if the element has the specified attribute.
      Parameters:
      name - The attribute name
      Returns:
      true if the attribute exists
    • removeAttribute

      public XmlElement removeAttribute(String name)
      Removes the specified attribute.
      Parameters:
      name - The attribute name to remove
      Returns:
      This element for method chaining
    • getText

      public String 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

      public boolean removeElement(XmlNode child)
      Removes the specified child node.
      Parameters:
      child - The child node to remove
      Returns:
      true if the child was found and removed
    • removeAllElements

      public XmlElement removeAllElements()
      Removes all child nodes.
      Returns:
      This element for method chaining
    • setTagName

      public XmlElement setTagName(String name)
      Sets the element tag name.
      Parameters:
      name - The new element tag name
      Returns:
      This element for method chaining
    • getElementById

      public XmlElement getElementById(String id)
      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

      public boolean containsElement(XmlNode child)
      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

      public XmlElement 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

      public XmlElement 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

      public String toXmlString(int indent)
      Description copied from interface: XmlNode
      Returns the XML string representation of this node with the specified indentation level.
      Specified by:
      toXmlString in interface XmlNode
      Parameters:
      indent - the indentation level (number of two-space indents).
      Returns:
      the formatted XML string.
    • toString

      public String toString()
      Overrides:
      toString in class Object