XmlNode Class
Use the XmlNode class to work with a node in an XML document.
The following are methods for XmlNode. All are instance methods.
-
addChildElement(name, namespace, prefix)
Creates a child element node for this node.
-
Creates a child comment node for this node.
-
Creates a child text node for this node.
-
getAttribute(key, keyNamespace)
Returns namespacePrefix:attributeValue for the given key and key namespace.
-
Returns the number of attributes for this node.
-
Returns the attribute key for the given index. Index values start at 0.
-
Returns the attribute key namespace for the given index.
-
getAttributeValue(key, keyNamespace)
Returns the attribute value for the given key and key namespace.
-
getAttributeValueNs(key, keyNamespace)
Returns the attribute value namespace for the given key and key namespace.
-
getChildElement(name, namespace)
Returns the child element node for the node with the given name and namespace.
-
Returns the child element nodes for this node. This doesn’t include child text or comment nodes.
-
Returns the child nodes for this node. This includes all node types.
-
Returns the element name.
-
Returns the namespace of the element.
-
Returns the namespace of the element for the given prefix.
-
Returns the node type.
-
Returns the parent of this element.
-
Returns the prefix of the given namespace.
-
Returns the text for this node.
-
insertBefore(newChild, refChild)
Inserts a new child node before the specified node.
-
removeAttribute(key, keyNamespace)
Removes the attribute with the given key and key namespace. Returns
trueif successful,falseotherwise. -
Removes the given child node.
-
Sets the key attribute value.
-
setAttributeNs(key, value, keyNamespace, valueNamespace)
Sets the key attribute value.
-
setNamespace(prefix, namespace)
Sets the namespace for the given prefix.
Creates a child element node for this node.
public Dom.XmlNode addChildElement(String name, String namespace, String prefix)
- name: Type: String The name argument can’t have a
nullvalue. - namespace: Type: String
- prefix: Type: String
Type: Dom.XmlNode
- If the namespace argument has a non-
nullvalue and the prefix argument isnull, the namespace is set as the default namespace. - If the prefix argument is
null, Salesforce automatically assigns a prefix for the element. The format of the automatic prefix isnsi, where nsi includes the number i. If the prefix argument is'', the namespace is set as the default namespace.
Creates a child comment node for this node.
public Dom.XmlNode addCommentNode(String text)
- text: Type: String The text argument can’t have a
nullvalue.
Type: Dom.XmlNode
Creates a child text node for this node.
public Dom.XmlNode addTextNode(String text)
- text: Type: String The text argument can’t have a
nullvalue.
Type: Dom.XmlNode
Returns namespacePrefix:attributeValue for the given key and key namespace.
public String getAttribute(String key, String keyNamespace)
Type: String
For example, for the <xyz a:b="c:d" /> element:
getAttributereturnsc:dgetAttributeValuereturnsd
Returns the number of attributes for this node.
public Integer getAttributeCount()
Type: Integer
Returns the attribute key for the given index. Index values start at 0.
public String getAttributeKeyAt(Integer index)
- index: Type: Integer
Type: String
Returns the attribute key namespace for the given index.
public String getAttributeKeyNsAt(Integer index)
- index: Type: Integer
Type: String
Returns the attribute value for the given key and key namespace.
public String getAttributeValue(String key, String keyNamespace)
Type: String
For example, for the <xyz a:b="c:d" /> element:
getAttributereturnsc:dgetAttributeValuereturnsd
Returns the attribute value namespace for the given key and key namespace.
public String getAttributeValueNs(String key, String keyNamespace)
Type: String
Returns the child element node for the node with the given name and namespace.
public Dom.XmlNode getChildElement(String name, String namespace)
Type: Dom.XmlNode
Returns the child element nodes for this node. This doesn’t include child text or comment nodes.
public Dom.XmlNode[] getChildElements()
Type: Dom.XmlNode[]
Returns the child nodes for this node. This includes all node types.
public Dom.XmlNode[] getChildren()
Type: Dom.XmlNode[]
Returns the element name.
public String getName()
Type: String
Returns the namespace of the element.
public String getNamespace()
Type: String
Returns the namespace of the element for the given prefix.
public String getNamespaceFor(String prefix)
- prefix: Type: String
Type: String
Returns the node type.
public Dom.XmlNodeType getNodeType()
Type: Dom.XmlNodeType
Uses XmlNodeType enum to return COMMENT, ELEMENT, or TEXT as the node type.
Returns the parent of this element.
public Dom.XmlNode getParent()
Type: Dom.XmlNode
Returns the prefix of the given namespace.
public String getPrefixFor(String namespace)
- namespace: Type: String The namespace argument can’t have a
nullvalue.
Type: String
Returns the text for this node.
public String getText()
Type: String
Inserts a new child node before the specified node.
public Dom.XmlNode insertBefore(Dom.XmlNode newChild, Dom.XmlNode refChild)
- newChild: Type: Dom.XmlNode The node to insert.
- refChild: Type: Dom.XmlNode The node before the new node.
Type: Dom.XmlNode
- If refChild is
null, newChild is inserted at the end of the list. - If refChild doesn’t exist, an exception is thrown.
Removes the attribute with the given key and key namespace. Returns true if successful, false otherwise.
public Boolean removeAttribute(String key, String keyNamespace)
Type: Boolean
Removes the given child node.
public Boolean removeChild(Dom.XmlNode childNode)
- childNode: Type: Dom.XmlNode
Type: Boolean
Sets the key attribute value.
public Void setAttribute(String key, String value)
Type: Void
Sets the key attribute value.
public Void setAttributeNs(String key, String value, String keyNamespace, String valueNamespace)
Type: Void
Sets the namespace for the given prefix.
public Void setNamespace(String prefix, String namespace)
Type: Void