---
title: XMLElement
framework: foundation
role: symbol
role_heading: Class
path: foundation/xmlelement
---

# XMLElement

The element nodes in an XML tree structure.

## Declaration

```swift
class XMLElement
```

## Mentioned in

setURI:

## Overview

Overview An XMLElement object may have child nodes, specifically comment nodes, processing-instruction nodes, text nodes, and other 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 node, XMLDTD node, namespace node, or attribute node as a child raises an exception. If you add a child node to an XMLElement object and that child already has a parent, 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:), 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.  |   |   |   |   |   |  NSXMLElement implements  isEqual(_:) to perform a deep comparison: two 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:) 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(_:)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:)](foundation/xmlelement/init(name:).md)
- [init(name:stringValue:)](foundation/xmlelement/init(name:stringvalue:).md)
- [init(name:uri:)](foundation/xmlelement/init(name:uri:)-1r286.md)
- [init(xmlString:)](foundation/xmlelement/init(xmlstring:)-7vkg7.md)
- [init(kind:options:)](foundation/xmlelement/init(kind:options:).md)

### Obtaining Child Elements

- [elements(forName:)](foundation/xmlelement/elements(forname:).md)
- [elements(forLocalName:uri:)](foundation/xmlelement/elements(forlocalname:uri:).md)

### Manipulating Child Elements

- [addChild(_:)](foundation/xmlelement/addchild(_:).md)
- [insertChild(_:at:)](foundation/xmlelement/insertchild(_:at:).md)
- [insertChildren(_:at:)](foundation/xmlelement/insertchildren(_:at:).md)
- [removeChild(at:)](foundation/xmlelement/removechild(at:).md)
- [replaceChild(at:with:)](foundation/xmlelement/replacechild(at:with:).md)
- [setChildren(_:)](foundation/xmlelement/setchildren(_:).md)
- [normalizeAdjacentTextNodesPreservingCDATA(_:)](foundation/xmlelement/normalizeadjacenttextnodespreservingcdata(_:).md)

### Handling Attributes

- [addAttribute(_:)](foundation/xmlelement/addattribute(_:).md)
- [attribute(forName:)](foundation/xmlelement/attribute(forname:).md)
- [attribute(forLocalName:uri:)](foundation/xmlelement/attribute(forlocalname:uri:).md)
- [attributes](foundation/xmlelement/attributes.md)
- [removeAttribute(forName:)](foundation/xmlelement/removeattribute(forname:).md)
- [setAttributesWith(_:)](foundation/xmlelement/setattributeswith(_:).md)
- [setAttributesAs(_:)](foundation/xmlelement/setattributesas(_:).md)

### Setting Element URI

- [setURI:](foundation/nsxmlnode-seturi.md)

### Handling Namespaces

- [addNamespace(_:)](foundation/xmlelement/addnamespace(_:).md)
- [namespaces](foundation/xmlelement/namespaces.md)
- [namespace(forPrefix:)](foundation/xmlelement/namespace(forprefix:).md)
- [removeNamespace(forPrefix:)](foundation/xmlelement/removenamespace(forprefix:).md)
- [resolveNamespace(forName:)](foundation/xmlelement/resolvenamespace(forname:).md)
- [resolvePrefix(forNamespaceURI:)](foundation/xmlelement/resolveprefix(fornamespaceuri:).md)

### Initializers

- [init(XMLString:)](foundation/xmlelement/init(xmlstring:)-1wgno.md)
- [init(name:URI:)](foundation/xmlelement/init(name:uri:)-67uti.md)

## Relationships

### Inherits From

- [XMLNode](foundation/xmlnode.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

## See Also

### Tree-Based Processing

- [XMLDTD](foundation/xmldtd.md)
- [XMLDTDNode](foundation/xmldtdnode.md)
- [XMLDocument](foundation/xmldocument.md)
- [XMLNode](foundation/xmlnode.md)
