XmlNode Class

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

Namespace

Dom

XmlNode Methods

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

addChildElement(name, namespace, prefix)

Creates a child element node for this node.

Signature

public Dom.XmlNode addChildElement(String name, String namespace, String prefix)

Parameters

name
Type: String
The name argument can't have a null value.
namespace
Type: String
prefix
Type: String

Return Value

Type: Dom.XmlNode

Usage

  • 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 i is a number.If the prefix argument is '', the namespace is set as the default namespace.

addCommentNode(text)

Creates a child comment node for this node.

Signature

public Dom.XmlNode addCommentNode(String text)

Parameters

text
Type: String
The text argument can't have a null value.

Return Value

Type: Dom.XmlNode

addTextNode(text)

Creates a child text node for this node.

Signature

public Dom.XmlNode addTextNode(String text)

Parameters

text
Type: String
The text argument can't have a null value.

Return Value

Type: Dom.XmlNode

getAttribute(key, keyNamespace)

Returns namespacePrefix:attributeValue for the given key and key namespace.

Signature

public String getAttribute(String key, String keyNamespace)

Parameters

key
Type: String
keyNamespace
Type: String

Return Value

Type: String

Example

For example, for the <xyz a:b="c:d" /> element:

  • getAttribute returns c:d
  • getAttributeValue returns d

getAttributeCount()

Returns the number of attributes for this node.

Signature

public Integer getAttributeCount()

Return Value

Type: Integer

getAttributeKeyAt(index)

Returns the attribute key for the given index. Index values start at 0.

Signature

public String getAttributeKeyAt(Integer index)

Parameters

index
Type: Integer

Return Value

Type: String

getAttributeKeyNsAt(index)

Returns the attribute key namespace for the given index.

Signature

public String getAttributeKeyNsAt(Integer index)

Parameters

index
Type: Integer

Return Value

Type: String

getAttributeValue(key, keyNamespace)

Returns the attribute value for the given key and key namespace.

Signature

public String getAttributeValue(String key, String keyNamespace)

Parameters

key
Type: String
keyNamespace
Type: String

Return Value

Type: String

Example

For example, for the <xyz a:b="c:d" /> element:

  • getAttribute returns c:d
  • getAttributeValue returns d

getAttributeValueNs(key, keyNamespace)

Returns the attribute value namespace for the given key and key namespace.

Signature

public String getAttributeValueNs(String key, String keyNamespace)

Parameters

key
Type: String
keyNamespace
Type: String

Return Value

Type: String

getChildElement(name, namespace)

Returns the child element node for the node with the given name and namespace.

Signature

public Dom.XmlNode getChildElement(String name, String namespace)

Parameters

name
Type: String
namespace
Type: String

Return Value

Type: Dom.XmlNode

getChildElements()

Returns the child element nodes for this node. This doesn't include child text or comment nodes.

Signature

public Dom.XmlNode[] getChildElements()

Return Value

Type: Dom.XmlNode[]

getChildren()

Returns the child nodes for this node. This includes all node types.

Signature

public Dom.XmlNode[] getChildren()

Return Value

Type: Dom.XmlNode[]

getName()

Returns the element name.

Signature

public String getName()

Return Value

Type: String

getNamespace()

Returns the namespace of the element.

Signature

public String getNamespace()

Return Value

Type: String

getNamespaceFor(prefix)

Returns the namespace of the element for the given prefix.

Signature

public String getNamespaceFor(String prefix)

Parameters

prefix
Type: String

Return Value

Type: String

getNodeType()

Returns the node type.

Signature

public Dom.XmlNodeType getNodeType()

Return Value

Type: Dom.XmlNodeType

Uses XmlNodeType enum to return COMMENT, ELEMENT, or TEXT as the node type.

getParent()

Returns the parent of this element.

Signature

public Dom.XmlNode getParent()

Return Value

Type: Dom.XmlNode

getPrefixFor(namespace)

Returns the prefix of the given namespace.

Signature

public String getPrefixFor(String namespace)

Parameters

namespace
Type: String
The namespace argument can't have a null value.

Return Value

Type: String

getText()

Returns the text for this node.

Signature

public String getText()

Return Value

Type: String

insertBefore(newChild, refChild)

Inserts a new child node before the specified node.

Signature

public Dom.XmlNode insertBefore(Dom.XmlNode newChild, Dom.XmlNode refChild)

Parameters

newChild
Type: Dom.XmlNode
The node to insert.
refChild
Type: Dom.XmlNode
The node before the new node.

Return Value

Type: Dom.XmlNode

Usage

  • If refChild is null, newChild is inserted at the end of the list.
  • If refChild doesn't exist, an exception is thrown.

removeAttribute(key, keyNamespace)

Removes the attribute with the given key and key namespace. Returns true if successful, false otherwise.

Signature

public Boolean removeAttribute(String key, String keyNamespace)

Parameters

key
Type: String
keyNamespace
Type: String

Return Value

Type: Boolean

removeChild(childNode)

Removes the given child node.

Signature

public Boolean removeChild(Dom.XmlNode childNode)

Parameters

childNode
Type: Dom.XmlNode

Return Value

Type: Boolean

setAttribute(key, value)

Sets the key attribute value.

Signature

public Void setAttribute(String key, String value)

Parameters

key
Type: String
value
Type: String

Return Value

Type: Void

setAttributeNs(key, value, keyNamespace, valueNamespace)

Sets the key attribute value.

Signature

public Void setAttributeNs(String key, String value, String keyNamespace, String valueNamespace)

Parameters

key
Type: String
value
Type: String
keyNamespace
Type: String
valueNamespace
Type: String

Return Value

Type: Void

setNamespace(prefix, namespace)

Sets the namespace for the given prefix.

Signature

public Void setNamespace(String prefix, String namespace)

Parameters

prefix
Type: String
namespace
Type: String

Return Value

Type: Void