A representation of an XML attribute.
// Reads the first and last name of each person and adds a new attribute with the full name. var xml = '<roster>' + '<person first="John" last="Doe"/>' + '<person first="Mary" last="Smith"/>' + '</roster>'; var document = XmlService.parse(xml); var people = document.getRootElement().getChildren('person'); for (var i = 0; i < people.length; i++) { var person = people[i]; var firstName = person.getAttribute('first').getValue(); var lastName = person.getAttribute('last').getValue(); person.setAttribute('full', firstName + ' ' + lastName); } xml = XmlService.getPrettyFormat().format(document); Logger.log(xml);
Methods
Method | Return type | Brief description |
---|---|---|
getName() | String | Gets the local name of the attribute. |
getNamespace() | Namespace | Gets the namespace for the attribute. |
getValue() | String | Gets the value of the attribute. |
setName(name) | Attribute | Sets the local name of the attribute. |
setNamespace(namespace) | Attribute | Sets the namespace for the attribute. |
setValue(value) | Attribute | Sets the value of the attribute. |
Detailed documentation
getName()
Gets the local name of the attribute. If the attribute has a namespace prefix, use getNamespace()
.getPrefix()
to get the prefix.
Return
String
— the local name of the attribute
getNamespace()
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, use setNamespace(namespace)
in conjunction with XmlService.getNamespace(prefix, uri)
.
Parameters
Name | Type | Description |
---|---|---|
name | String | the local name to set |
Return
Attribute
— the attribute, for chaining
setNamespace(namespace)
setValue(value)
Sets the value of the attribute.
Parameters
Name | Type | Description |
---|---|---|
value | String | the value to set |
Return
Attribute
— the attribute, for chaining