<!--
{
  "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

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

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

[`convenience init(name: String, stringValue: String?)`](/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: String, uri: String?)`](/documentation/Foundation/XMLElement/init(name:uri:)-1r286)

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

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

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

[`convenience init(kind: XMLNode.Kind, options: XMLNode.Options)`](/documentation/Foundation/XMLElement/init(kind:options:))

### Obtaining Child Elements

[`func elements(forName: String) -> [XMLElement]`](/documentation/Foundation/XMLElement/elements(forName:))

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

[`func elements(forLocalName: String, uri: String?) -> [XMLElement]`](/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

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

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

[`func insertChild(XMLNode, at: Int)`](/documentation/Foundation/XMLElement/insertChild(_:at:))

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

[`func insertChildren([XMLNode], at: Int)`](/documentation/Foundation/XMLElement/insertChildren(_:at:))

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

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

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

[`func replaceChild(at: Int, with: XMLNode)`](/documentation/Foundation/XMLElement/replaceChild(at:with:))

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

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

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

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

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

### Handling Attributes

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

Adds an attribute node to the receiver.

[`func attribute(forName: String) -> XMLNode?`](/documentation/Foundation/XMLElement/attribute(forName:))

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

[`func attribute(forLocalName: String, uri: String?) -> XMLNode?`](/documentation/Foundation/XMLElement/attribute(forLocalName:uri:))

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

[`var attributes: [XMLNode]?`](/documentation/Foundation/XMLElement/attributes)

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

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

Removes an attribute node identified by name.

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

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

[`func setAttributesAs([AnyHashable : Any])`](/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

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

Adds a namespace node to the receiver.

[`var namespaces: [XMLNode]?`](/documentation/Foundation/XMLElement/namespaces)

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

[`func namespace(forPrefix: String) -> XMLNode?`](/documentation/Foundation/XMLElement/namespace(forPrefix:))

Returns the namespace node with a specified prefix.

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

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

[`func resolveNamespace(forName: String) -> XMLNode?`](/documentation/Foundation/XMLElement/resolveNamespace(forName:))

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

[`func resolvePrefix(forNamespaceURI: String) -> String?`](/documentation/Foundation/XMLElement/resolvePrefix(forNamespaceURI:))

Returns the prefix associated with the specified URI.

### Initializers

[`init(XMLString: String) throws`](/documentation/Foundation/XMLElement/init(XMLString:)-1wgno)

[`init(name: String, URI: String?)`](/documentation/Foundation/XMLElement/init(name:URI:)-67uti)

## Relationships

### Conforms To

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Hashable`](/documentation/Swift/Hashable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`Equatable`](/documentation/Swift/Equatable)

[`Sendable`](/documentation/Swift/Sendable)

### Inherits From

[`XMLNode`](/documentation/Foundation/XMLNode)

---

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)