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()
创建所有未附加节点的副本,这些节点是 {@code Element} 节点的直接子节点。
返回
Content[]
- 所有节点的未附加副本,这些节点是 {@code Element} 节点的直接子节点
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()
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)
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
节点,用于串联