Class Attribute

  • An Attribute object represents an XML attribute and is used in conjunction with XmlService.

  • Attributes have methods to get and set their name, namespace, and value.

  • The provided example demonstrates how to parse XML, retrieve attributes, and add a new attribute with a combined value.

Attribute

A representation of an XML attribute.

// Reads the first and last name of each person and adds a new attribute with// the full name.letxml='<roster>'+'<person first="John" last="Doe"/>'+'<person first="Mary" last="Smith"/>'+'</roster>';constdocument=XmlService.parse(xml);constpeople=document.getRootElement().getChildren('person');for(leti=0;i <people.length;i++){constperson=people[i];constfirstName=person.getAttribute('first').getValue();constlastName=person.getAttribute('last').getValue();person.setAttribute('full',`${firstName}${lastName}`);}xml=XmlService.getPrettyFormat().format(document);Logger.log(xml);

Methods

MethodReturn typeBrief description
getName()StringGets the local name of the attribute.
getNamespace()NamespaceGets the namespace for the attribute.
getValue()StringGets the value of the attribute.
setName(name)AttributeSets the local name of the attribute.
setNamespace(namespace)AttributeSets the namespace for the attribute.
setValue(value)AttributeSets the value of the attribute.

Detailed documentation

getName()

Gets the local name of the attribute. If the attribute has a namespace prefix, usegetNamespace().getPrefix() to get the prefix.

Return

String — the local name of the attribute


getNamespace()

Gets the namespace for the attribute.

Return

Namespace — the namespace for the attribute


getValue()

Gets the value of the attribute.

Return

String — the value of the attribute


setName(name)

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

Parameters

NameTypeDescription
nameStringthe local name to set

Return

Attribute — the attribute, for chaining


setNamespace(namespace)

Sets the namespace for the attribute. The namespace must have a prefix.

Parameters

NameTypeDescription
namespaceNamespacethe namespace to set

Return

Attribute — the attribute, for chaining


setValue(value)

Sets the value of the attribute.

Parameters

NameTypeDescription
valueStringthe value to set

Return

Attribute — the attribute, 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.