Class Attribute

属性

XML 属性的表示形式。

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

方法

方法返回类型简介
getName()String获取属性的本地名称。
getNamespace()Namespace|null获取属性的命名空间。
getValue()String获取属性的值。
setName(name)Attribute设置属性的本地名称。
setNamespace(namespace)Attribute为属性设置命名空间。
setValue(value)Attribute设置属性的值。

详细文档

getName()

获取属性的本地名称。如果属性具有命名空间前缀,请使用 getNamespace()getPrefix() 获取前缀。

返回

String - 属性的本地名称。


getNamespace()

获取属性的命名空间。

返回

Namespace|null - 属性的命名空间。


getValue()

获取属性的值。

返回

String - 属性的值。


setName(name)

设置属性的本地名称。如需为属性设置命名空间前缀,请结合使用 setNamespace(namespace)XmlService.getNamespace(prefix, uri)

参数

名称类型说明
nameString要设置的本地名称。

返回

Attribute - 用于链式调用的属性。


setNamespace(namespace)

为属性设置命名空间。命名空间必须具有前缀。

参数

名称类型说明
namespaceNamespace要设置的命名空间。

返回

Attribute - 用于链式调用的属性。


setValue(value)

设置属性的值。

参数

名称类型说明
valueString要设置的值。

返回

Attribute - 用于链式调用的属性。