表示 XML Element
节点。
// Adds up the values listed in a sample XML document and adds a new element with the total. var xml = '<things>' + '<plates>12</plates>' + '<bowls>18</bowls>' + '<cups>25</cups>' + '</things>'; var document = XmlService.parse(xml); var root = document.getRootElement(); var items = root.getChildren(); var total = 0; for (var i = 0; i < items.length; i++) { total += Number(items[i].getText()); } var totalElement = XmlService.createElement('total').setText(total); root.addContent(totalElement); xml = XmlService.getPrettyFormat().format(document); Logger.log(xml);
方法
方法 | 返回类型 | 简介 |
---|---|---|
addContent(content) | Element | 将指定节点作为 Element 节点的最后一个子项附加。 |
addContent(index, content) | Element | 在属于 Element 节点直接子节点的所有节点中,在给定索引处插入给定节点。 |
cloneContent() | Content[] | 创建所有属于 {@code Element} 节点直接子节点的节点的未附加副本。 |
detach() | Content | 将节点与其父 Element 节点分离。 |
getAllContent() | Content[] | 获取属于 {@code Element} 节点的直接子节点的所有节点。 |
getAttribute(name) | Attribute | 获取具有指定名称但没有命名空间的该 Element 节点的属性。 |
getAttribute(name, namespace) | Attribute | 获取具有指定名称和命名空间的此 Element 节点的属性。 |
getAttributes() | Attribute[] | 按照属性在文档中出现的顺序获取此 Element 节点的所有属性。 |
getChild(name) | Element | 获取具有指定名称的第一个 Element 节点,但没有为该 Element 节点直接子项的命名空间。 |
getChild(name, namespace) | Element | 获取具有指定名称和命名空间的第一个 Element 节点,该节点是该 Element 节点的直接子节点。 |
getChildText(name) | String | 获取具有指定名称和无命名空间的节点的文本值(如果该节点是 Element 节点的直接子节点)。 |
getChildText(name, namespace) | String | 获取具有指定名称和命名空间的节点的文本值(如果该节点是 Element 节点的直接子节点)。 |
getChildren() | Element[] | 按照节点在文档中出现的顺序获取属于此Element 节点直接子节点的所有Element 节点。 |
getChildren(name) | Element[] | 按照这些节点在文档中出现的顺序,获取具有给定名称且没有命名空间的所有 Element 节点,这些节点是此 Element 节点的直接子节点。 |
getChildren(name, namespace) | Element[] | 获取具有指定名称和命名空间的所有 Element 节点,这些节点是此 Element 节点的直接子节点(按照它们在文档中出现的顺序)。 |
getContent(index) | Content | 从属于 {@code Element} 节点的直接子节点的所有节点中获取给定索引处的节点。 |
getContentSize() | Integer | 获取属于 {@code Element} 节点直接子节点的节点数。 |
getDescendants() | Content[] | 按照节点在文档中出现的顺序,获取作为 {@code Element} 节点的直接或间接子节点的所有节点。 |
getDocument() | Document | 获取包含 {@code Element} 节点的 XML 文档。 |
getName() | String | 获取 Element 节点的本地名称。 |
getNamespace() | Namespace | 获取 Element 节点的命名空间。 |
getNamespace(prefix) | Namespace | 获取具有 Element 节点给定前缀的命名空间。 |
getParentElement() | Element | 获取节点的父级 Element 节点。 |
getQualifiedName() | String | 获取 Element 节点的本地名称和命名空间前缀,格式为 [namespacePrefix]:[localName] 。 |
getText() | String | 获取 Element 节点的文本值。 |
getValue() | String | 按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。 |
isAncestorOf(other) | Boolean | 确定此 Element 节点是给定 Element 节点的直接父节点还是间接父节点。 |
isRootElement() | Boolean | 确定 Element 节点是否为文档的根节点。 |
removeAttribute(attribute) | Boolean | 移除此 Element 节点的指定属性(如果存在)。 |
removeAttribute(attributeName) | Boolean | 移除具有指定名称和无命名空间的此 Element 节点的属性(如果存在此类属性)。 |
removeAttribute(attributeName, namespace) | Boolean | 移除具有给定名称和命名空间的此 Element 节点的属性(如果存在此类属性)。 |
removeContent() | Content[] | 移除所有属于 {@code Element} 节点直接子节点的节点。 |
removeContent(content) | Boolean | 移除指定节点(如果该节点是 {@code Element} 节点的直接子节点)。 |
removeContent(index) | Content | 从属于 {@code Element} 节点的直接子节点的所有节点中移除给定索引处的节点。 |
setAttribute(attribute) | Element | 设置此 Element 节点的给定属性。 |
setAttribute(name, value) | Element | 使用此 Element 节点的指定名称、值设置属性,但不指定命名空间。 |
setAttribute(name, value, namespace) | Element | 使用给定的名称、值和命名空间设置此 Element 节点的属性。 |
setName(name) | Element | 设置 Element 节点的本地名称。 |
setNamespace(namespace) | Element | 设置 Element 节点的命名空间。 |
setText(text) | Element | 设置 Element 节点的文本值。 |
详细文档
addContent(content)
将指定节点作为 Element
节点的最后一个子项附加。content
参数可以是 Element
对象,也可以是与 ContentType
中列出的类型对应的任何节点对象。
参数
名称 | 类型 | 说明 |
---|---|---|
content | Content | 要附加的节点 |
弃踢回攻
Element
- Element
节点,用于链接
addContent(index, content)
在属于 Element
节点直接子节点的所有节点中,在给定索引处插入给定节点。content
参数可以是 Element
对象,也可以是与 ContentType
中列出的类型对应的任何节点对象。
参数
名称 | 类型 | 说明 |
---|---|---|
index | Integer | 是在 Element 节点直接子节点的所有节点中插入该节点的索引 |
content | Content | 要插入的节点 |
弃踢回攻
Element
- Element
节点,用于链接
cloneContent()
getAllContent()
getAttribute(name)
获取具有指定名称但没有命名空间的该 Element
节点的属性。如果没有此类属性,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 属性的名称 |
弃踢回攻
Attribute
- 属性,如果没有具有给定名称和命名空间的属性,则为 null
getAttribute(name, namespace)
getAttributes()
getChild(name)
获取具有指定名称的第一个 Element
节点,但没有为该 Element
节点直接子项的命名空间。如果没有此类节点,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 子 Element 节点的名称 |
弃踢回攻
Element
- Element
节点,如果不存在具有指定名称且没有命名空间的直接子 Element
节点,则为 null
getChild(name, namespace)
getChildText(name)
获取具有指定名称和无命名空间的节点的文本值(如果该节点是 Element
节点的直接子节点)。如果没有此类节点,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 子节点的名称 |
弃踢回攻
String
- 子节点的文本值,如果没有具有指定名称且没有命名空间的直接子节点,则返回 null
getChildText(name, namespace)
获取具有指定名称和命名空间的节点的文本值(如果该节点是 Element
节点的直接子节点)。如果没有此类节点,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 子节点的名称 |
namespace | Namespace | 该子节点的命名空间 |
弃踢回攻
String
- 子节点的文本值,如果没有具有指定名称和命名空间的直接子节点,则返回 null
getChildren()
按照节点在文档中出现的顺序获取属于此Element
节点直接子节点的所有Element
节点。
弃踢回攻
Element[]
- 由所有 Element
节点构成的数组,这些节点是此 Element
节点的直接子节点
getChildren(name)
按照这些节点在文档中出现的顺序,获取具有给定名称且没有命名空间的所有 Element
节点,这些节点是此 Element
节点的直接子节点。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 子 Element 节点的名称 |
弃踢回攻
Element[]
- 一个由具有指定名称且没有命名空间的所有 Element
节点组成的数组,这些节点是该 Element
节点的直接子节点
getChildren(name, namespace)
getContent(index)
从属于 {@code Element} 节点的直接子节点的所有节点中获取给定索引处的节点。如果指定索引处没有任何节点,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
index | Integer | 作为 {@code Element} 节点直接子节点的所有节点中的节点索引 |
弃踢回攻
Content
- 节点,如果指定索引中没有节点,则返回 null
getContentSize()
获取属于 {@code Element} 节点直接子节点的节点数。
弃踢回攻
Integer
- 作为 {@code Element} 节点的直接子节点的个数
getDescendants()
按照节点在文档中出现的顺序,获取作为 {@code Element} 节点的直接或间接子节点的所有节点。
弃踢回攻
Content[]
- 一个数组,其中包含作为 {@code Element} 节点的直接或间接子节点的所有节点
getDocument()
getName()
获取 Element
节点的本地名称。如果节点具有命名空间前缀,请使用 getQualifiedName()
或 getNamespace()
。getPrefix()
可获取前缀。
弃踢回攻
String
- Element
节点的本地名称
getNamespace()
getNamespace(prefix)
getQualifiedName()
获取 Element
节点的本地名称和命名空间前缀,格式为 [namespacePrefix]:[localName]
。如果节点没有命名空间前缀,请使用 getName()
。
弃踢回攻
String
- Element
节点的本地名称和命名空间前缀,格式为 [namespacePrefix]:[localName]
getText()
获取 Element
节点的文本值。
弃踢回攻
String
- Element
节点的文本值
getValue()
按照节点在文档中出现的顺序获取作为节点的直接或间接子节点的所有节点的文本值。
弃踢回攻
String
- 所有是节点直接或间接子节点的文本值
isAncestorOf(other)
确定此 Element
节点是给定 Element
节点的直接父节点还是间接父节点。
参数
名称 | 类型 | 说明 |
---|---|---|
other | Element | 另一个 Element 节点 |
弃踢回攻
Boolean
- 如果此 Element
节点是给定 Element
节点的直接或间接父节点,则为 true
;如果不是,则为 false
isRootElement()
确定 Element
节点是否为文档的根节点。
弃踢回攻
Boolean
- 如果 Element
节点是文档的根节点,则为 true
;如果不是,则为 false
removeAttribute(attribute)
移除此 Element
节点的指定属性(如果存在)。
参数
名称 | 类型 | 说明 |
---|---|---|
attribute | Attribute | 属性 |
弃踢回攻
Boolean
- 如果属性存在但已移除,则为 true
;如果不存在,则为 false
removeAttribute(attributeName)
移除具有指定名称和无命名空间的此 Element
节点的属性(如果存在此类属性)。
参数
名称 | 类型 | 说明 |
---|---|---|
attributeName | String | 属性的名称 |
弃踢回攻
Boolean
- 如果属性存在但已移除,则为 true
;如果不存在,则为 false
removeAttribute(attributeName, namespace)
移除具有给定名称和命名空间的此 Element
节点的属性(如果存在此类属性)。
参数
名称 | 类型 | 说明 |
---|---|---|
attributeName | String | 属性的名称 |
namespace | Namespace | 属性的命名空间 |
弃踢回攻
Boolean
- 如果属性存在但已移除,则为 true
;如果不存在,则为 false
removeContent()
removeContent(content)
移除指定节点(如果该节点是 {@code Element} 节点的直接子节点)。content
参数可以是 Element
对象,也可以是与 ContentType
中列出的类型对应的任何节点对象。
参数
名称 | 类型 | 说明 |
---|---|---|
content | Content | 要移除的节点 |
弃踢回攻
Boolean
- 如果该节点是直接子节点且已被移除,则为 true
;如果不是,则为 false
removeContent(index)
从属于 {@code Element} 节点的直接子节点的所有节点中移除给定索引处的节点。如果指定索引处没有任何节点,此方法会返回 null
。
参数
名称 | 类型 | 说明 |
---|---|---|
index | Integer | 作为 {@code Element} 节点直接子节点的所有节点中的节点索引 |
弃踢回攻
Content
- 已移除的节点,如果指定索引中没有节点,则返回 null
setAttribute(attribute)
setAttribute(name, value)
使用此 Element
节点的指定名称、值设置属性,但不指定命名空间。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要设置的属性名称 |
value | String | 要设置的属性值 |
弃踢回攻
Element
- Element
节点,用于链接
setAttribute(name, value, namespace)
setName(name)
设置 Element
节点的本地名称。如需为节点设置命名空间前缀,请将 setNamespace(namespace)
与 XmlService.getNamespace(prefix, uri)
结合使用。
参数
名称 | 类型 | 说明 |
---|---|---|
name | String | 要设置的本地名称 |
弃踢回攻
Element
- Element
节点,用于链接
setNamespace(namespace)
setText(text)
设置 Element
节点的文本值。如果节点已包含文本值或任何子节点,则此方法会覆盖旧内容。如需改为附加或插入内容,请使用 addContent(content)
或 addContent(index, content)
。
参数
名称 | 类型 | 说明 |
---|---|---|
text | String | 要设置的文本 |
弃踢回攻
Element
- Element
节点,用于链接