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

# XMLNode

The nodes in the abstract, logical tree structure that represents an XML document.

```
class XMLNode
```

## Overview

Node objects can be of different kinds, corresponding to the following markup constructs in an XML document: element, attribute, text, processing instruction, namespace, and comment. In addition, a document-node object (specifically, an instance of [`XMLDocument`](/documentation/Foundation/XMLDocument)) represents an XML document in its entirety. [`XMLNode`](/documentation/Foundation/XMLNode) objects can also represent document type declarations as well as declarations in Document Type Definitions (DTDs). Class factory methods of [`XMLNode`](/documentation/Foundation/XMLNode) enable you to create nodes of each kind. Only document, element, and DTD nodes may have child nodes.

Among the XML family of classes (excluding [`XMLParser`](/documentation/Foundation/XMLParser)) the [`XMLNode`](/documentation/Foundation/XMLNode) class is the base class. Inheriting from it are the classes [`XMLElement`](/documentation/Foundation/XMLElement), [`XMLDocument`](/documentation/Foundation/XMLDocument), [`XMLDTD`](/documentation/Foundation/XMLDTD), and [`XMLDTDNode`](/documentation/Foundation/XMLDTDNode). [`XMLNode`](/documentation/Foundation/XMLNode) specifies the interface common to all XML node objects and defines common node behavior and attributes, for example hierarchy level, node name and value, tree traversal, and the ability to emit representative XML markup text.

### Subclassing Notes

You can subclass [`XMLNode`](/documentation/Foundation/XMLNode) if you want nodes of kinds different from the supported ones, You can also create a subclass with more specialized attributes or behavior than [`XMLNode`](/documentation/Foundation/XMLNode).

#### Methods to Override

To subclass [`XMLNode`](/documentation/Foundation/XMLNode) you need to override the primary initializer, [`init(kind:options:)`](/documentation/Foundation/XMLNode/init(kind:options:)), 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/XMLNode/kind-swift.property``                 |``doc://com.apple.foundation/documentation/Foundation/XMLNode/parent``    |
|----------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/name``                                |``doc://com.apple.foundation/documentation/Foundation/XMLNode/child(at:)``|
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/name``                                |``doc://com.apple.foundation/documentation/Foundation/XMLNode/childCount``|
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/objectValue``                         |``doc://com.apple.foundation/documentation/Foundation/XMLNode/children``  |
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/objectValue``                         |``doc://com.apple.foundation/documentation/Foundation/XMLNode/detach()``  |
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/stringValue``                         |``doc://com.apple.foundation/documentation/Foundation/XMLNode/localName`` |
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/setStringValue(_:resolvingEntities:)``|``doc://com.apple.foundation/documentation/Foundation/XMLNode/prefix``    |
|``doc://com.apple.foundation/documentation/Foundation/XMLNode/index``                               |``doc://com.apple.foundation/documentation/Foundation/XMLNode/uri``       |

By default [`XMLNode`](/documentation/Foundation/XMLNode) implements the `NSObject` <doc://com.apple.documentation/documentation/ObjectiveC/NSObjectProtocol/isEqual(_:)> method to perform a deep comparison: two [`XMLNode`](/documentation/Foundation/XMLNode) objects are not considered equal unless they have the same name, same child nodes, same attributes, and so on. The comparison looks at the node and its children, but does not include the node’s parent. 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 [`XMLDocument`](/documentation/Foundation/XMLDocument) 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 [`XMLNode`](/documentation/Foundation/XMLNode) that represents CDATA sections—then you can walk the tree after it has been created and insert the new node where appropriate.

## Topics

### Creating and Initializing Node Objects

[`-  initWithKind:`](/documentation/Foundation/XMLNode/init(kind:))

Returns an `NSXMLNode` instance initialized with the constant indicating node kind.

[`-  initWithKind:options:`](/documentation/Foundation/XMLNode/init(kind:options:))

Returns an `NSXMLNode` instance initialized with the constant indicating node kind and one or more initialization options.

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

Returns an empty document node.

[`+  documentWithRootElement:`](/documentation/Foundation/XMLNode/document(withRootElement:))

Returns an [`XMLDocument`](/documentation/Foundation/XMLDocument) object initialized with a given root element.

[`+  elementWithName:`](/documentation/Foundation/XMLNode/element(withName:))

Returns an [`XMLElement`](/documentation/Foundation/XMLElement) object with a given tag identifier, or name

[`+  elementWithName:children:attributes:`](/documentation/Foundation/XMLNode/element(withName:children:attributes:))

Returns an [`XMLElement`](/documentation/Foundation/XMLElement) object with the given tag (name), attributes, and children.

[`+  elementWithName:stringValue:`](/documentation/Foundation/XMLNode/element(withName:stringValue:))

Returns an [`XMLElement`](/documentation/Foundation/XMLElement) object with a single text-node child containing the specified text.

[`+  elementWithName:URI:`](/documentation/Foundation/XMLNode/element(withName:uri:))

Returns an element whose fully qualified name is specified.

[`+  attributeWithName:stringValue:`](/documentation/Foundation/XMLNode/attribute(withName:stringValue:))

Returns an `NSXMLNode` object representing an attribute node with a given name and string.

[`+  attributeWithName:URI:stringValue:`](/documentation/Foundation/XMLNode/attribute(withName:uri:stringValue:))

Returns an `NSXMLNode` object representing an attribute node with a given qualified name and string.

[`+  textWithStringValue:`](/documentation/Foundation/XMLNode/text(withStringValue:))

Returns an `NSXMLNode` object representing a text node with specified content.

[`+  commentWithStringValue:`](/documentation/Foundation/XMLNode/comment(withStringValue:))

Returns an [`XMLNode`](/documentation/Foundation/XMLNode) object representing a comment node containing given text.

[`+  namespaceWithName:stringValue:`](/documentation/Foundation/XMLNode/namespace(withName:stringValue:))

Returns an `NSXMLNode` object representing a namespace with a specified name and URI.

[`+  DTDNodeWithXMLString:`](/documentation/Foundation/XMLNode/dtdNode(withXMLString:))

Returns a [`XMLDTDNode`](/documentation/Foundation/XMLDTDNode) object representing the DTD declaration for an element, attribute, entity, or notation based on a given string.

[`+  predefinedNamespaceForPrefix:`](/documentation/Foundation/XMLNode/predefinedNamespace(forPrefix:))

Returns an `NSXMLNode` object representing one of the predefined namespaces with the specified prefix.

[`+  processingInstructionWithName:stringValue:`](/documentation/Foundation/XMLNode/processingInstruction(withName:stringValue:))

Returns an `NSXMLNode` object representing a processing instruction with a specified name and value.

### Managing XML Node Objects

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

Returns the index of the receiver identifying its position relative to its sibling nodes.

[`kind`](/documentation/Foundation/XMLNode/kind-swift.property)

Returns the kind of node the receiver is as a constant of type [`XMLNode.Kind`](/documentation/Foundation/XMLNode/Kind-swift.enum).

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

Returns the nesting level of the receiver within the tree hierarchy.

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

Returns the name of the receiver.

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

Returns the object value of the receiver.

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

Returns the content of the receiver as a string value.

[`-  setStringValue:resolvingEntities:`](/documentation/Foundation/XMLNode/setStringValue(_:resolvingEntities:))

Sets the content of the receiver as a string value and, optionally, resolves character references, predefined entities, and user-defined entities as declared in the associated DTD.

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

Sets the URI of the receiver.

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

Returns the URI associated with the receiver.

### Navigating the Tree of Nodes

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

Returns the [`XMLDocument`](/documentation/Foundation/XMLDocument) object containing the root element and representing the XML document as a whole.

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

Returns the parent node of the receiver.

[`-  childAtIndex:`](/documentation/Foundation/XMLNode/child(at:))

Returns the child node of the receiver at the specified location.

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

Returns the number of child nodes the receiver has.

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

Returns an immutable array containing the child nodes of the receiver (as `NSXMLNode` objects).

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

Returns the next `NSXMLNode` object in document order.

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

Returns the next `NSXMLNode` object that is a sibling node to the receiver.

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

Returns the previous `NSXMLNode` object in document order.

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

Returns the previous `NSXMLNode` object that is a sibling node to the receiver.

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

Detaches the receiver from its parent node.

### Emitting Node Content

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

Returns the string representation of the receiver as it would appear in an XML document.

[`-  XMLStringWithOptions:`](/documentation/Foundation/XMLNode/xmlString(options:))

Returns the string representation of the receiver as it would appear in an XML document, with one or more output options specified.

[`-  canonicalXMLStringPreservingComments:`](/documentation/Foundation/XMLNode/canonicalXMLStringPreservingComments(_:))

Returns a string object encapsulating the receiver’s XML in canonical form.

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

### Executing Queries

[`-  nodesForXPath:error:`](/documentation/Foundation/XMLNode/nodes(forXPath:))

Returns the nodes resulting from executing an XPath query upon the receiver.

[`-  objectsForXQuery:error:`](/documentation/Foundation/XMLNode/objects(forXQuery:))

Returns the objects resulting from executing an XQuery query upon the receiver.

[`-  objectsForXQuery:constants:error:`](/documentation/Foundation/XMLNode/objects(forXQuery:constants:))

Returns the objects resulting from executing an XQuery query upon the receiver.

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

Returns the XPath expression identifying the receiver’s location in the document tree.

### Managing Namespaces

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

Returns the local name of the receiver.

[`+  localNameForName:`](/documentation/Foundation/XMLNode/localName(forName:))

Returns the local name from the specified qualified name.

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

Returns the prefix of the receiver’s name.

[`+  prefixForName:`](/documentation/Foundation/XMLNode/prefix(forName:))

Returns the prefix from the specified qualified name.

### Constants

[`Kind`](/documentation/Foundation/XMLNode/Kind-swift.enum)

`NSXMLNode` declares the following constants of type NSXMLNodeKind for specifying a node’s kind in the initializer methods [`init(kind:)`](/documentation/Foundation/XMLNode/init(kind:)) and [`init(kind:options:)`](/documentation/Foundation/XMLNode/init(kind:options:)):

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

These constants are input and output options for all `NSXMLNode` objects (unless otherwise indicated), including [`XMLDocument`](/documentation/Foundation/XMLDocument) objects. You can specify these options in the `NSXMLNode` methods [`init(kind:options:)`](/documentation/Foundation/XMLNode/init(kind:options:)) and [`xmlString(options:)`](/documentation/Foundation/XMLNode/xmlString(options:)).

[`NSXMLNodeOptionsNone`](/documentation/Foundation/NSXMLNodeOptions/NSXMLNodeOptionsNone)

No options are requested for this input or output action.

## Relationships

### Inherited By

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

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

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

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

### Conforms To

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

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

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

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

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

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

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

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)