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

# XMLDocument

An XML document as internalized into a logical tree structure.

```
class XMLDocument
```

## Overview

An [`XMLDocument`](/documentation/Foundation/XMLDocument) object can have multiple child nodes but only one element, the root element. Any other node must be a [`XMLNode`](/documentation/Foundation/XMLNode) object representing a comment or a processing instruction. If you attempt to add any other kind of child node to an [`XMLDocument`](/documentation/Foundation/XMLDocument) object, such as an attribute, namespace, another document object, or an element other than the root, [`XMLDocument`](/documentation/Foundation/XMLDocument) raises an exception. If you add a valid child node and that object already has a parent, [`XMLDocument`](/documentation/Foundation/XMLDocument) raises an exception. An [`XMLDocument`](/documentation/Foundation/XMLDocument) object may also have document-global attributes, such as XML version, character encoding, referenced DTD, and MIME type.

The initializers of the [`XMLDocument`](/documentation/Foundation/XMLDocument) class read an external source of XML, whether it be a local file or remote website, parse it, and process it into the tree representation. You can also construct an [`XMLDocument`](/documentation/Foundation/XMLDocument) programmatically. There are accessor methods for getting and setting document attributes, methods for transforming documents using XSLT, a method for dynamically validating a document, and methods for printing out the content of an [`XMLDocument`](/documentation/Foundation/XMLDocument) as XML, XHTML, HTML, or plain text.

The [`XMLDocument`](/documentation/Foundation/XMLDocument) class is thread-safe as long as any given instance is used only in one thread.

### Subclassing Notes

#### Methods to Override

To subclass `NSXMLDocument` you need to override the primary initializer, [`init(data:options:)`](/documentation/Foundation/XMLDocument/init(data: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.

- [`rootElement()`](/documentation/Foundation/XMLDocument/rootElement())
- [`setChildren(_:)`](/documentation/Foundation/XMLDocument/setChildren(_:))
- [`removeChild(at:)`](/documentation/Foundation/XMLDocument/removeChild(at:))
- [`insertChild(_:at:)`](/documentation/Foundation/XMLDocument/insertChild(_:at:))
- [`characterEncoding`](/documentation/Foundation/XMLDocument/characterEncoding)
- [`characterEncoding`](/documentation/Foundation/XMLDocument/characterEncoding)
- [`documentContentKind`](/documentation/Foundation/XMLDocument/documentContentKind)
- [`documentContentKind`](/documentation/Foundation/XMLDocument/documentContentKind)
- [`dtd`](/documentation/Foundation/XMLDocument/dtd)
- [`mimeType`](/documentation/Foundation/XMLDocument/mimeType)
- [`isStandalone`](/documentation/Foundation/XMLDocument/isStandalone)
- [`version`](/documentation/Foundation/XMLDocument/version)
- [`version`](/documentation/Foundation/XMLDocument/version)

By default `NSXMLDocument` implements the `NSObject` <doc://com.apple.documentation/documentation/ObjectiveC/NSObjectProtocol/isEqual(_:)> method to perform a deep comparison: two `NSXMLDocument` objects are not considered equal unless they have the same name, same child nodes, same attributes, and so on. The comparison does not consider the parent node (and hence the node’s location). 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.

## Topics

### Initializing NSXMLDocument Objects

[`init(contentsOf:options:)`](/documentation/Foundation/XMLDocument/init(contentsOf:options:))

Initializes and returns an NSXMLDocument object created from the XML or HTML contents of a URL-referenced source

[`init(data:options:)`](/documentation/Foundation/XMLDocument/init(data:options:))

Initializes and returns an `NSXMLDocument` object created from an [`NSData`](/documentation/Foundation/NSData) object.

[`init(rootElement:)`](/documentation/Foundation/XMLDocument/init(rootElement:))

Returns an `NSXMLDocument` object initialized with a single child, the root element.

[`init(xmlString:options:)`](/documentation/Foundation/XMLDocument/init(xmlString:options:)-65m2r)

Initializes and returns an `NSXMLDocument` object created from a string containing XML markup text.

[`replacementClass(for:)`](/documentation/Foundation/XMLDocument/replacementClass(for:))

Overridden by subclasses to substitute a custom class for an NSXML class that the parser uses to create node instances.

### Managing Document Attributes

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

Sets the character encoding of the receiver to `encoding`,

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

Sets the kind of output content for the receiver.

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

Returns an [`XMLDTD`](/documentation/Foundation/XMLDTD) object representing the internal DTD associated with the receiver.

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

Sets a Boolean value that specifies whether the receiver represents a standalone XML document.

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

Returns the MIME type for the receiver.

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

Sets the version of the receiver’s XML.

### Setting Document URI

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

Sets the URI of the receiver.

### Managing the Root Element

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

Returns the root element of the receiver.

[`setRootElement(_:)`](/documentation/Foundation/XMLDocument/setRootElement(_:))

Set the root element of the receiver.

### Adding and Removing Child Nodes

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

Adds a child node after the last of the receiver’s existing children.

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

Inserts a node object at specified position in the receiver’s array of children.

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

Inserts an array of children at a specified position in the receiver’s array of children.

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

Removes the child node of the receiver located at a specified position in its array of children.

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

Replaces the child node of the receiver located at a specified position in its array of children with another node.

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

Sets the child nodes of the receiver.

### Transforming a Document Using XSLT

[`object(byApplyingXSLT:arguments:)`](/documentation/Foundation/XMLDocument/object(byApplyingXSLT:arguments:))

Applies the XSLT pattern rules and templates (specified as a data object) to the receiver and returns a document object containing transformed XML or HTML markup.

[`object(byApplyingXSLTString:arguments:)`](/documentation/Foundation/XMLDocument/object(byApplyingXSLTString:arguments:))

Applies the XSLT pattern rules and templates (specified as a string) to the receiver and returns a document object containing transformed XML or HTML markup.

[`objectByApplyingXSLT(at:arguments:)`](/documentation/Foundation/XMLDocument/objectByApplyingXSLT(at:arguments:))

Applies the XSLT pattern rules and templates located at a specified URL to the receiver and returns a document object containing transformed XML markup or an [`NSData`](/documentation/Foundation/NSData) object containing plain text, RTF text, and so on.

### Writing a Document as XML Data

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

Returns the XML string representation of the receiver—that is, the entire document—encapsulated in a data object.

[`xmlData(options:)`](/documentation/Foundation/XMLDocument/xmlData(options:))

Returns the XML string representation of the receiver—that is, the entire document—encapsulated in a data object.

### Validating a Document

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

Validates the document against the governing schema and returns whether the document conforms to the schema.

### Constants

[Input and Output Options](/documentation/Foundation/input_and_output_options)

Input and output options specifically intended for `NSXMLDocument` objects.

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

Type used to define the kind of document content.

[Document Content Types](/documentation/Foundation/document-content-types)

Define document types.



---

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)