Class XmlElement
java.lang.Object
cloud.opencode.base.xml.XmlElement
- All Implemented Interfaces:
XmlNode
XML Element - Wrapper for DOM Element with fluent API
XML 元素 - 提供流式 API 的 DOM Element 封装
This class wraps a DOM Element and provides convenient methods for accessing and manipulating XML content.
此类封装 DOM Element,并提供便捷的方法来访问和操作 XML 内容。
Features | 主要功能:
- Fluent API for element access and modification - 元素访问和修改的流式 API
- Attribute read/write operations - 属性读写操作
- Child element navigation and manipulation - 子元素导航和操作
- XPath queries relative to this element - 相对于此元素的 XPath 查询
- Type-safe text conversion - 类型安全的文本转换
- Object binding support - 对象绑定支持
Usage Examples | 使用示例:
XmlElement element = XmlElement.of(domElement);
// Get element info
String name = element.getName();
String text = element.getText();
// Access attributes
String id = element.getAttribute("id");
Map<String, String> attrs = element.getAttributes();
// Access children
XmlElement child = element.getChild("name");
List<XmlElement> items = element.getChildren("item");
// XPath queries
String value = element.xpath("./name/text()");
List<XmlElement> results = element.xpathList(".//item");
Security | 安全性:
- Thread-safe: No (wraps mutable DOM Element) - 线程安全: 否(封装可变的 DOM Element)
- Null-safe: No (null inputs throw exceptions) - 空值安全: 否(空值输入抛出异常)
- Since:
- JDK 25, opencode-base-xml V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface XmlNode
XmlNode.NodeType -
Method Summary
Modifier and TypeMethodDescriptionaddChild(XmlElement child) Adds an existing element as a child.Adds a new child element.Adds a new child element with text content.<T> TBinds this element to an object.booleangetAttribute(String name) Returns the attribute value.<T> TgetAttribute(String name, Class<T> clazz) Returns the attribute value converted to the specified type.getAttribute(String name, String defaultValue) Returns the attribute value or default.Returns all attributes as a Map.Returns the first child element with the specified name.intReturns the number of child elements.Returns all child elements.getChildren(String name) Returns all child elements with the specified name.getChildText(String name) Returns the text content of a child element.getChildText(String name, String defaultValue) Returns the text content of a child element or default.Returns the underlying DOM Element.Returns the local name (without namespace prefix).getName()Returns the node name.Returns the namespace URI.getNode()Returns the underlying DOM Node.Returns the parent element.Returns the namespace prefix.getText()Returns the text content.Returns the text content or default value if null/empty.<T> TReturns the text content converted to the specified type.Returns the text content trimmed.booleanhasAttribute(String name) Returns whether this element has the specified attribute.booleanReturns whether this element has a child with the specified name.inthashCode()booleanReturns whether this element has a parent element.booleanhasText()Returns whether this node has any text content.static XmlElementCreates an XmlElement from a DOM Element.static XmlElementCreates an XmlElement from a DOM Node (if it's an Element).removeAttribute(String name) Removes an attribute.removeChild(String name) Removes child elements with the specified name.Removes all child elements.setAttribute(String name, String value) Sets an attribute value.Sets the text content.toMap()Converts this node to a Map.toString()toXml()Converts this node to XML string.toXml(int indent) Converts this node to XML string with indentation.Evaluates an XPath expression relative to this element.Evaluates an XPath expression and returns element list.
-
Method Details
-
of
Creates an XmlElement from a DOM Element. 从 DOM Element 创建 XmlElement。- Parameters:
element- the DOM Element | DOM Element- Returns:
- the XmlElement | XmlElement
-
of
Creates an XmlElement from a DOM Node (if it's an Element). 从 DOM Node 创建 XmlElement(如果是 Element)。- Parameters:
node- the DOM Node | DOM Node- Returns:
- the XmlElement | XmlElement
- Throws:
OpenXmlException- if node is not an Element | 如果节点不是 Element 则抛出异常
-
getElement
Returns the underlying DOM Element. 返回底层的 DOM Element。- Returns:
- the DOM Element | DOM Element
-
getNode
-
getName
-
getLocalName
Description copied from interface:XmlNodeReturns the local name (without namespace prefix). 返回本地名称(不含命名空间前缀)。- Specified by:
getLocalNamein interfaceXmlNode- Returns:
- the local name | 本地名称
-
getNamespaceUri
Description copied from interface:XmlNodeReturns the namespace URI. 返回命名空间 URI。- Specified by:
getNamespaceUriin interfaceXmlNode- Returns:
- the namespace URI, or null if not set | 命名空间 URI,如果未设置则返回 null
-
getPrefix
-
getText
-
getTextTrim
Description copied from interface:XmlNodeReturns the text content trimmed. 返回去除空白的文本内容。- Specified by:
getTextTrimin interfaceXmlNode- Returns:
- the trimmed text content | 去除空白的文本内容
-
hasText
-
setText
Sets the text content. 设置文本内容。- Parameters:
text- the text content | 文本内容- Returns:
- this element for chaining | 此元素以便链式调用
-
getTextAs
Returns the text content converted to the specified type. 返回转换为指定类型的文本内容。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
clazz- the target type | 目标类型- Returns:
- the converted value | 转换后的值
-
getText
-
getAttribute
-
getAttribute
-
getAttribute
-
getAttributes
-
setAttribute
Sets an attribute value. 设置属性值。- Parameters:
name- the attribute name | 属性名value- the attribute value | 属性值- Returns:
- this element for chaining | 此元素以便链式调用
-
removeAttribute
Removes an attribute. 移除属性。- Parameters:
name- the attribute name | 属性名- Returns:
- this element for chaining | 此元素以便链式调用
-
hasAttribute
Returns whether this element has the specified attribute. 返回此元素是否具有指定的属性。- Parameters:
name- the attribute name | 属性名- Returns:
- true if attribute exists | 如果属性存在则返回 true
-
getChild
Returns the first child element with the specified name. 返回具有指定名称的第一个子元素。- Parameters:
name- the child element name | 子元素名称- Returns:
- the child element, or null if not found | 子元素,如果未找到则返回 null
-
getChildren
Returns all child elements with the specified name. 返回具有指定名称的所有子元素。- Parameters:
name- the child element name | 子元素名称- Returns:
- the list of child elements | 子元素列表
-
getChildren
Returns all child elements. 返回所有子元素。- Returns:
- the list of all child elements | 所有子元素列表
-
getChildText
-
getChildText
-
hasChild
Returns whether this element has a child with the specified name. 返回此元素是否具有指定名称的子元素。- Parameters:
name- the child element name | 子元素名称- Returns:
- true if child exists | 如果子元素存在则返回 true
-
getChildCount
public int getChildCount()Returns the number of child elements. 返回子元素数量。- Returns:
- the child count | 子元素数量
-
addChild
Adds a new child element. 添加新的子元素。- Parameters:
name- the element name | 元素名称- Returns:
- the new child element | 新子元素
-
addChild
Adds a new child element with text content. 添加带文本内容的新子元素。- Parameters:
name- the element name | 元素名称text- the text content | 文本内容- Returns:
- the new child element | 新子元素
-
addChild
Adds an existing element as a child. 将现有元素添加为子元素。- Parameters:
child- the child element to add | 要添加的子元素- Returns:
- this element for chaining | 此元素以便链式调用
-
removeChild
Removes child elements with the specified name. 移除具有指定名称的子元素。- Parameters:
name- the child element name | 子元素名称- Returns:
- this element for chaining | 此元素以便链式调用
-
removeChildren
Removes all child elements. 移除所有子元素。- Returns:
- this element for chaining | 此元素以便链式调用
-
getParent
Returns the parent element. 返回父元素。- Returns:
- the parent element, or null if this is the root | 父元素,如果是根元素则返回 null
-
hasParent
public boolean hasParent()Returns whether this element has a parent element. 返回此元素是否有父元素。- Returns:
- true if has parent | 如果有父元素则返回 true
-
xpath
-
xpathList
Evaluates an XPath expression and returns element list. 求值 XPath 表达式并返回元素列表。- Parameters:
xpath- the XPath expression | XPath 表达式- Returns:
- the list of matching elements | 匹配元素列表
-
bind
Binds this element to an object. 将此元素绑定到对象。- Type Parameters:
T- the type parameter | 类型参数- Parameters:
clazz- the target type | 目标类型- Returns:
- the bound object | 绑定的对象
-
toMap
-
toXml
-
toXml
Description copied from interface:XmlNodeConverts this node to XML string with indentation. 将此节点转换为带缩进的 XML 字符串。 -
toString
-
equals
-
hashCode
-