<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/XMLElement",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSXMLElement"
  },
  "title" : "XMLElement"
}
-->

# XMLElement

The element nodes in an XML tree structure.

```
class XMLElement
```

## Overview

An [`XMLElement`](/documentation/Foundation/XMLElement) object may have child nodes, specifically comment nodes, processing-instruction nodes, text nodes, and other [`XMLElement`](/documentation/Foundation/XMLElement) nodes. It may also have attribute nodes and namespace nodes associated with it (however, namespace and attribute nodes are not considered children). Any attempt to add a [`XMLDocument`](/documentation/Foundation/XMLDocument) node, [`XMLDTD`](/documentation/Foundation/XMLDTD) node, namespace node, or attribute node as a child raises an exception. If you add a child node to an [`XMLElement`](/documentation/Foundation/XMLElement) object and that child already has a parent, [`XMLElement`](/documentation/Foundation/XMLElement) raises an exception; the child must be detached or copied first.

### Subclassing Notes

You can subclass `NSXMLElement` if you want element nodes with more specialized attributes or behavior, for example, paragraph and font attributes that specify how the string value of the element should appear.

#### Methods to Override

To subclass `NSXMLElement` you need to override the primary initializer, [`init(name:uri:)`](/documentation/Foundation/XMLElement/init(name:uri:)-1r286), and the methods listed below. In most cases, you need only invoke the superclass implementation, adding any subclass-specific code before or after the invocation, as necessary.

|``doc://com.apple.foundation/documentation/Foundation/XMLElement/addAttribute(_:)``            |``doc://com.apple.foundation/documentation/Foundation/XMLElement/removeNamespace(forPrefix:)``|
|-----------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|
|``doc://com.apple.foundation/documentation/Foundation/XMLElement/removeAttribute(forName:)``   |``doc://com.apple.foundation/documentation/Foundation/XMLElement/namespaces``                 |
|``doc://com.apple.foundation/documentation/Foundation/XMLElement/attributes``                  |``doc://com.apple.foundation/documentation/Foundation/XMLElement/namespaces``                 |
|``doc://com.apple.foundation/documentation/Foundation/XMLElement/attribute(forLocalName:uri:)``|``doc://com.apple.foundation/documentation/Foundation/XMLElement/insertChild(_:at:)``         |
|``doc://com.apple.foundation/documentation/Foundation/XMLElement/attributes``                  |``doc://com.apple.foundation/documentation/Foundation/XMLElement/removeChild(at:)``           |
|``doc://com.apple.foundation/documentation/Foundation/XMLElement/addNamespace(_:)``            |``doc://com.apple.foundation/documentation/Foundation/XMLElement/setChildren(_:)``            |

`NSXMLElement` implements  <doc://com.apple.documentation/documentation/ObjectiveC/NSObjectProtocol/isEqual(_:)> to perform a deep comparison: two [`XMLDocument`](/documentation/Foundation/XMLDocument) objects are not considered equal unless they have the same name, same child nodes, same attributes, and so on. If you want a different standard of comparison, override `isEqual:`.

#### Special Considerations

Because of the architecture and data model of NSXML, when it parses and processes a source of XML it cannot know about your subclass unless you override the class method [`replacementClass(for:)`](/documentation/Foundation/XMLDocument/replacementClass(for:)) to return your custom class in place of an NSXML class. If your custom class has no direct NSXML counterpart—for example, it is a subclass of `NSXMLNode` that represents CDATA sections—then you can walk the tree after it has been created and insert the new node where appropriate.

Note that you can safely set the root element of the XML document (using the `NSXMLDocument` [`setRootElement(_:)`](/documentation/Foundation/XMLDocument/setRootElement(_:))method) to be an instance of your subclass because this method only checks to see if the added node is of an element kind (`NSXMLElementKind`). These precautions do not apply, of course, if you are creating an XML tree programmatically.

## Topics

### Initializing NSXMLElement Objects

[`init(name:)`](/documentation/Foundation/XMLElement/init(name:))

Returns an `NSXMLElement` object initialized with the specified name.

[`init(name:stringValue:)`](/documentation/Foundation/XMLElement/init(name:stringValue:))

Returns an `NSXMLElement` object initialized with a specified name and a single text-node child containing a specified value.

[`init(name:uri:)`](/documentation/Foundation/XMLElement/init(name:uri:)-1r286)

Returns an `NSXMLElement` object initialized with the specified name and URI.

[`init(xmlString:)`](/documentation/Foundation/XMLElement/init(xmlString:)-7vkg7)

Returns an `NSXMLElement` object created from a specified string containing XML markup.

[`init(kind:options:)`](/documentation/Foundation/XMLElement/init(kind:options:))

### Obtaining Child Elements

[`elements(forName:)`](/documentation/Foundation/XMLElement/elements(forName:))

Returns the child element nodes (as `NSXMLElement` objects) of the receiver that have a specified name.

[`elements(forLocalName:uri:)`](/documentation/Foundation/XMLElement/elements(forLocalName:uri:))

Returns the child element nodes (as `NSXMLElement` objects) of the receiver that are matched with the specified local name and URI.

### Manipulating Child Elements

[`addChild(_:)`](/documentation/Foundation/XMLElement/addChild(_:))

Adds a child node at the end of the receiver’s current list of children.

[`insertChild(_:at:)`](/documentation/Foundation/XMLElement/insertChild(_:at:))

Inserts a new child node at a specified location in the receiver’s list of child nodes.

[`insertChildren(_:at:)`](/documentation/Foundation/XMLElement/insertChildren(_:at:))

Inserts an array of child nodes at a specified location in the receiver’s list of children.

[`removeChild(at:)`](/documentation/Foundation/XMLElement/removeChild(at:))

Removes the child node of the receiver identified by a given index.

[`replaceChild(at:with:)`](/documentation/Foundation/XMLElement/replaceChild(at:with:))

Replaces a child node at a specified location with another child node.

[`setChildren(_:)`](/documentation/Foundation/XMLElement/setChildren(_:))

Sets all child nodes of the receiver at once, replacing any existing children.

[`normalizeAdjacentTextNodesPreservingCDATA(_:)`](/documentation/Foundation/XMLElement/normalizeAdjacentTextNodesPreservingCDATA(_:))

Coalesces adjacent text nodes of the receiver that you have explicitly added, optionally including CDATA sections.

### Handling Attributes

[`addAttribute(_:)`](/documentation/Foundation/XMLElement/addAttribute(_:))

Adds an attribute node to the receiver.

[`attribute(forName:)`](/documentation/Foundation/XMLElement/attribute(forName:))

Returns the attribute node of the receiver with the specified name.

[`attribute(forLocalName:uri:)`](/documentation/Foundation/XMLElement/attribute(forLocalName:uri:))

Returns the attribute node of the receiver that is identified by a local name and URI.

[`attributes`](/documentation/Foundation/XMLElement/attributes)

Sets all attributes of the receiver at once, replacing any existing attribute nodes.

[`removeAttribute(forName:)`](/documentation/Foundation/XMLElement/removeAttribute(forName:))

Removes an attribute node identified by name.

[`setAttributesWith(_:)`](/documentation/Foundation/XMLElement/setAttributesWith(_:))

Sets the attributes of the receiver based on the key-value pairs specified in the passed dictionary.

[`setAttributesAs(_:)`](/documentation/Foundation/XMLElement/setAttributesAs(_:))

Sets the attributes of the receiver based on the key-value pairs specified in the passed-in dictionary.

### Setting Element URI

[setURI:](/documentation/Foundation/nsxmlnode-seturi)

Sets the URI of the receiver.

### Handling Namespaces

[`addNamespace(_:)`](/documentation/Foundation/XMLElement/addNamespace(_:))

Adds a namespace node to the receiver.

[`namespaces`](/documentation/Foundation/XMLElement/namespaces)

Sets all of the namespace nodes of the receiver at once, replacing any existing namespace nodes.

[`namespace(forPrefix:)`](/documentation/Foundation/XMLElement/namespace(forPrefix:))

Returns the namespace node with a specified prefix.

[`removeNamespace(forPrefix:)`](/documentation/Foundation/XMLElement/removeNamespace(forPrefix:))

Removes a namespace node that is identified by a given prefix.

[`resolveNamespace(forName:)`](/documentation/Foundation/XMLElement/resolveNamespace(forName:))

Returns the namespace node with the prefix matching the given qualified name.

[`resolvePrefix(forNamespaceURI:)`](/documentation/Foundation/XMLElement/resolvePrefix(forNamespaceURI:))

Returns the prefix associated with the specified URI.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)