Class Element

  • The Element object represents an XML Element node and provides methods for manipulating its content, attributes, and relationships within an XML document.

  • You can add, remove, and retrieve content (including child elements and other nodes) from an Element using methods like addContent, removeContent, getAllContent, getChildren, and getContent.

  • Attributes of an Element can be accessed, set, and removed using methods like getAttribute, getAttributes, setAttribute, and removeAttribute.

  • The name and text value of an Element can be retrieved and set using getName, getQualifiedName, getText, setText, and getValue.

  • Methods are available to check the parent and document of an Element, determine if it is the root element, and detach it from its parent.

Element

A representation of an XMLElement node.

// Adds up the values listed in a sample XML document and adds a new element// with the total.letxml='<things>'+'<plates>12</plates>'+'<bowls>18</bowls>'+'<cups>25</cups>'+'</things>';constdocument=XmlService.parse(xml);constroot=document.getRootElement();constitems=root.getChildren();lettotal=0;for(leti=0;i <items.length;i++){total+=Number(items[i].getText());}consttotalElement=XmlService.createElement('total').setText(total);root.addContent(totalElement);xml=XmlService.getPrettyFormat().format(document);Logger.log(xml);

Methods

MethodReturn typeBrief description
addContent(content)ElementAppends the given node as the last child of theElement node.
addContent(index, content)ElementInserts the given node at the given index among all nodes that are immediate children of theElement node.
cloneContent()Content[]Creates unattached copies of all nodes that are immediate children of the {@code Element} node.
detach()ContentDetaches the node from its parentElement node.
getAllContent()Content[]Gets all nodes that are immediate children of the {@code Element} node.
getAttribute(name)AttributeGets the attribute for thisElement node with the given name and no namespace.
getAttribute(name, namespace)AttributeGets the attribute for thisElement node with the given name and namespace.
getAttributes()Attribute[]Gets all attributes for thisElement node, in the order they appear in the document.
getChild(name)ElementGets the firstElement node with the given name and no namespace that is an immediatechild of thisElement node.
getChild(name, namespace)ElementGets the firstElement node with the given name and namespace that is an immediatechild of thisElement node.
getChildText(name)StringGets the text value of the node with the given name and no namespace, if the node is animmediate child of theElement node.
getChildText(name, namespace)StringGets the text value of the node with the given name and namespace, if the node is an immediatechild of theElement node.
getChildren()Element[]Gets allElement nodes that are immediate children of thisElement node, in theorder they appear in the document.
getChildren(name)Element[]Gets allElement nodes with the given name and no namespace that are immediate childrenof thisElement node, in the order they appear in the document.
getChildren(name, namespace)Element[]Gets allElement nodes with the given name and namespace that are immediate children ofthisElement node, in the order they appear in the document.
getContent(index)ContentGets the node at the given index among all nodes that are immediate children of the{@code Element} node.
getContentSize()IntegerGets the number of nodes that are immediate children of the {@code Element} node.
getDescendants()Content[]Gets all nodes that are direct or indirect children of the {@code Element} node, in the order theyappear in the document.
getDocument()DocumentGets the XML document that contains the {@code Element} node.
getName()StringGets the local name of theElement node.
getNamespace()NamespaceGets the namespace for theElement node.
getNamespace(prefix)NamespaceGets the namespace with the given prefix for theElement node.
getParentElement()ElementGets the node's parentElement node.
getQualifiedName()StringGets the local name and namespace prefix of theElement node, in the form[namespacePrefix]:[localName].
getText()StringGets the text value of theElement node.
getValue()StringGets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.
isAncestorOf(other)BooleanDetermines whether thisElement node is a direct or indirect parent of a givenElement node.
isRootElement()BooleanDetermines whether theElement node is the document's root node.
removeAttribute(attribute)BooleanRemoves the given attribute for thisElement node, if such an attribute exists.
removeAttribute(attributeName)BooleanRemoves the attribute for thisElement node with the given name and no namespace, ifsuch an attribute exists.
removeAttribute(attributeName, namespace)BooleanRemoves the attribute for thisElement node with the given name and namespace, if suchan attribute exists.
removeContent()Content[]Removes all nodes that are immediate children of the {@code Element} node.
removeContent(content)BooleanRemoves the given node, if the node is an immediate child of the {@code Element} node.
removeContent(index)ContentRemoves the node at the given index among all nodes that are immediate children of the{@code Element} node.
setAttribute(attribute)ElementSets the given attribute for thisElement node.
setAttribute(name, value)ElementSets the attribute for thisElement node with the given name, value, and no namespace.
setAttribute(name, value, namespace)ElementSets the attribute for thisElement node with the given name, value, and namespace.
setName(name)ElementSets the local name of theElement node.
setNamespace(namespace)ElementSets the namespace for theElement node.
setText(text)ElementSets the text value of theElement node.

Detailed documentation

addContent(content)

Appends the given node as the last child of theElement node. Thecontentargument can be aElement object or any node object that corresponds to a typelisted inContentType.

Parameters

NameTypeDescription
contentContentthe node to append

Return

Element — theElement node, for chaining


addContent(index, content)

Inserts the given node at the given index among all nodes that are immediate children of theElement node. Thecontent argument can be aElement object or anynode object that corresponds to a type listed inContentType.

Parameters

NameTypeDescription
indexIntegerthe index at which to insert the node among all nodes that are immediate children of theElement node
contentContentthe node to insert

Return

Element — theElement node, for chaining


cloneContent()

Creates unattached copies of all nodes that are immediate children of the {@code Element} node.

Return

Content[] — an array of unattached copies of all nodes that are immediate children of the {@code Element} node


detach()

Detaches the node from its parentElement node. If the node does not have a parent,this method has no effect.

Return

Content — the detached node


getAllContent()

Gets all nodes that are immediate children of the {@code Element} node.

Return

Content[] — an array of all nodes that are immediate children of the {@code Element} node


getAttribute(name)

Gets the attribute for thisElement node with the given name and no namespace. If thereis no such attribute, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the attribute

Return

Attribute — the attribute, ornull if there is no attribute with the given name and no namespace


getAttribute(name, namespace)

Gets the attribute for thisElement node with the given name and namespace. If there isno such node, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the attribute
namespaceNamespacethe namespace of the attribute

Return

Attribute — the attribute, ornull if there is no attribute with the given name and namespace


getAttributes()

Gets all attributes for thisElement node, in the order they appear in the document.

Return

Attribute[] — an array of all attributes for thisElement node


getChild(name)

Gets the firstElement node with the given name and no namespace that is an immediatechild of thisElement node. If there is no such node, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the childElement node

Return

Element — theElement node, ornull if there is no immediate childElement node with the given name and no namespace


getChild(name, namespace)

Gets the firstElement node with the given name and namespace that is an immediatechild of thisElement node. If there is no such node, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the childElement node
namespaceNamespacethe namespace of the childElement node

Return

Element — theElement node, ornull if there is no immediate childElement node with the given name and namespace


getChildText(name)

Gets the text value of the node with the given name and no namespace, if the node is animmediate child of theElement node. If there is no such node, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the child node

Return

String — the text value of the child node, ornull if there is no immediate child node with the given name and no namespace


getChildText(name, namespace)

Gets the text value of the node with the given name and namespace, if the node is an immediatechild of theElement node. If there is no such node, this method returnsnull.

Parameters

NameTypeDescription
nameStringthe name of the child node
namespaceNamespacethe namespace of the child node

Return

String — the text value of the child node, ornull if there is no immediate child node with the given name and namespace


getChildren()

Gets allElement nodes that are immediate children of thisElement node, in theorder they appear in the document.

Return

Element[] — an array of allElement nodes that are immediate children of thisElement node


getChildren(name)

Gets allElement nodes with the given name and no namespace that are immediate childrenof thisElement node, in the order they appear in the document.

Parameters

NameTypeDescription
nameStringthe name of the childElement nodes

Return

Element[] — an array of allElement nodes with the given name and no namespace that are immediate children of thisElement node


getChildren(name, namespace)

Gets allElement nodes with the given name and namespace that are immediate children ofthisElement node, in the order they appear in the document.

Parameters

NameTypeDescription
nameStringthe name of the childElement nodes
namespaceNamespacethe namespace of the childElement nodes

Return

Element[] — an array of allElement nodes with the given name and namespace that are immediate children of thisElement node


getContent(index)

Gets the node at the given index among all nodes that are immediate children of the{@code Element} node. If there is no node at the given index, this method returnsnull.

Parameters

NameTypeDescription
indexIntegerthe index for the node among all nodes that are immediate children of the {@code Element} node

Return

Content — the node, ornull if there is no node at the given index


getContentSize()

Gets the number of nodes that are immediate children of the {@code Element} node.

Return

Integer — the number of nodes that are immediate children of the {@code Element} node


getDescendants()

Gets all nodes that are direct or indirect children of the {@code Element} node, in the order theyappear in the document.

Return

Content[] — an array of all nodes that are direct or indirect children of the {@code Element} node


getDocument()

Gets the XML document that contains the {@code Element} node.

Return

Document — the document that contains the {@code Element} node


getName()

Gets the local name of theElement node. If the node has a namespace prefix, usegetQualifiedName() orgetNamespace().getPrefix() toget the prefix.

Return

String — the local name of theElement node


getNamespace()

Gets the namespace for theElement node.

Return

Namespace — the namespace for theElement node


getNamespace(prefix)

Gets the namespace with the given prefix for theElement node.

Parameters

NameTypeDescription
prefixStringthe prefix for the namespace

Return

Namespace — the namespace with the given prefix for theElement node


getParentElement()

Gets the node's parentElement node. If the node does not have a parent, this methodreturnsnull.

Return

Element — the parentElement node


getQualifiedName()

Gets the local name and namespace prefix of theElement node, in the form[namespacePrefix]:[localName]. If the node does not have a namespace prefix, usegetName().

Return

String — the local name and namespace prefix of theElement node, in the form[namespacePrefix]:[localName]


getText()

Gets the text value of theElement node.

Return

String — the text value of theElement node


getValue()

Gets the text value of all nodes that are direct or indirect children of the node, in the orderthey appear in the document.

Return

String — the text value of all nodes that are direct or indirect children of the node


isAncestorOf(other)

Determines whether thisElement node is a direct or indirect parent of a givenElement node.

Parameters

NameTypeDescription
otherElementthe otherElement node

Return

Booleantrue if thisElement node is a direct or indirect parent of the givenElement node;false if not


isRootElement()

Determines whether theElement node is the document's root node.

Return

Booleantrue if theElement node is the document's root node;false if not


removeAttribute(attribute)

Removes the given attribute for thisElement node, if such an attribute exists.

Parameters

NameTypeDescription
attributeAttributethe attribute

Return

Booleantrue if the attribute existed and was removed;false if not


removeAttribute(attributeName)

Removes the attribute for thisElement node with the given name and no namespace, ifsuch an attribute exists.

Parameters

NameTypeDescription
attributeNameStringthe name of the attribute

Return

Booleantrue if the attribute existed and was removed;false if not


removeAttribute(attributeName, namespace)

Removes the attribute for thisElement node with the given name and namespace, if suchan attribute exists.

Parameters

NameTypeDescription
attributeNameStringthe name of the attribute
namespaceNamespacethe namespace of the attribute

Return

Booleantrue if the attribute existed and was removed;false if not


removeContent()

Removes all nodes that are immediate children of the {@code Element} node.

Return

Content[] — an array of all nodes that were immediate children of the {@code Element} node before they were removed


removeContent(content)

Removes the given node, if the node is an immediate child of the {@code Element} node. Thecontent argument can be aElement object or any node object that corresponds to atype listed inContentType.

Parameters

NameTypeDescription
contentContentthe node to remove

Return

Booleantrue if the node was an immediate child and was removed;false if not


removeContent(index)

Removes the node at the given index among all nodes that are immediate children of the{@code Element} node. If there is no node at the given index, this method returnsnull.

Parameters

NameTypeDescription
indexIntegerthe index for the node among all nodes that are immediate children of the {@code Element} node

Return

Content — the node that was removed, ornull if there is no node at the given index


setAttribute(attribute)

Sets the given attribute for thisElement node.

Parameters

NameTypeDescription
attributeAttributethe attribute to set

Return

Element — theElement node, for chaining


setAttribute(name, value)

Sets the attribute for thisElement node with the given name, value, and no namespace.

Parameters

NameTypeDescription
nameStringthe name of the attribute to set
valueStringthe value of the attribute to set

Return

Element — theElement node, for chaining


setAttribute(name, value, namespace)

Sets the attribute for thisElement node with the given name, value, and namespace.

Parameters

NameTypeDescription
nameStringthe name of the attribute to set
valueStringthe value of the attribute to set
namespaceNamespacethe namespace of the attribute to set

Return

Element — theElement node, for chaining


setName(name)

Sets the local name of theElement node. To set a namespace prefix for the node, usesetNamespace(namespace) in conjunction withXmlService.getNamespace(prefix, uri).

Parameters

NameTypeDescription
nameStringthe local name to set

Return

Element — theElement node, for chaining


setNamespace(namespace)

Sets the namespace for theElement node.

Parameters

NameTypeDescription
namespaceNamespacethe namespace to set

Return

Element — theElement node, for chaining


setText(text)

Sets the text value of theElement node. If the node already contains a text value orany child nodes, this method overwrites the old content. To append or insert content instead,useaddContent(content) oraddContent(index, content).

Parameters

NameTypeDescription
textStringthe text to set

Return

Element — theElement node, for chaining

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.