XmlNode Class

Use the XmlNode class to work with a node in an XML document.

Dom

The following are methods for XmlNode. All are instance methods.

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 null value.
  • namespace: Type: String
  • prefix: Type: String

Type: Dom.XmlNode

  • If the namespace argument has a non-null value and the prefix argument is null, 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 is nsi, 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 null value.

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 null value.

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:

  • getAttribute returns c:d
  • getAttributeValue returns d

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)

Type: String

Returns the attribute key namespace for the given index.

public String getAttributeKeyNsAt(Integer index)

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:

  • getAttribute returns c:d
  • getAttributeValue returns d

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)

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 null value.

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)

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)

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