<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/XMLParser",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSXMLParser"
  },
  "title" : "XMLParser"
}
-->

# XMLParser

An event driven parser of XML documents (including DTD declarations).

```
class XMLParser
```

## Overview

An [`XMLParser`](/documentation/Foundation/XMLParser) notifies its delegate about the items (elements, attributes, CDATA blocks, comments, and so on) that it encounters as it processes an XML document. It does not itself do anything with those parsed items except report them. It also reports parsing errors. For convenience, an [`XMLParser`](/documentation/Foundation/XMLParser) object in the following descriptions is sometimes referred to as a parser object. Unless used in a callback, the [`XMLParser`](/documentation/Foundation/XMLParser) is a thread-safe class as long as any given instance is only used in one thread.

> Note:
> Namespace support was implemented in ``doc://com.apple.foundation/documentation/Foundation/XMLParser`` starting in macOS 10.4. Namespace-related methods of ``doc://com.apple.foundation/documentation/Foundation/XMLParser`` prior to this version have no effect.

## Topics

### Initializing a Parser Object

[`init(contentsOf:)`](/documentation/Foundation/XMLParser/init(contentsOf:))

Initializes a parser with the XML content referenced by the given URL.

[`init(data:)`](/documentation/Foundation/XMLParser/init(data:))

Initializes a parser with the XML contents encapsulated in a given data object.

[`init(stream:)`](/documentation/Foundation/XMLParser/init(stream:))

Initializes a parser with the XML contents from the specified stream and parses it.

### Managing Delegates

[`delegate`](/documentation/Foundation/XMLParser/delegate)

A delegate object that receives messages about the parsing process.

### Managing Parser Behavior

[`shouldProcessNamespaces`](/documentation/Foundation/XMLParser/shouldProcessNamespaces)

A Boolean value that determines whether the parser reports the namespaces and qualified names of elements.

[`shouldReportNamespacePrefixes`](/documentation/Foundation/XMLParser/shouldReportNamespacePrefixes)

A Boolean value that determines whether the parser reports the prefixes indicating the scope of namespace declarations.

[`shouldResolveExternalEntities`](/documentation/Foundation/XMLParser/shouldResolveExternalEntities)

A Boolean value that determines whether the parser reports declarations of external entities.

### Parsing

[`parse()`](/documentation/Foundation/XMLParser/parse())

Starts the event-driven parsing operation.

[`abortParsing()`](/documentation/Foundation/XMLParser/abortParsing())

Stops the parser object.

[`parserError`](/documentation/Foundation/XMLParser/parserError)

An [`NSError`](/documentation/Foundation/NSError) object from which you can obtain information about a parsing error.

### Obtaining Parser State

[`columnNumber`](/documentation/Foundation/XMLParser/columnNumber)

The column number of the XML document being processed by the parser.

[`lineNumber`](/documentation/Foundation/XMLParser/lineNumber)

The line number of the XML document being processed by the parser.

[`publicID`](/documentation/Foundation/XMLParser/publicID)

The public identifier of the external entity referenced in the XML document.

[`systemID`](/documentation/Foundation/XMLParser/systemID)

The system identifier of the external entity referenced in the XML document.

### Constants

[`XMLParser.ExternalEntityResolvingPolicy`](/documentation/Foundation/XMLParser/ExternalEntityResolvingPolicy-swift.enum)

Defines the external entity resolving policy used by an `NSXMLParser` instance.

[`errorDomain`](/documentation/Foundation/XMLParser/errorDomain)

Indicates an error in XML parsing.

[`XMLParser.ErrorCode`](/documentation/Foundation/XMLParser/ErrorCode)

The following error codes are defined by `NSXMLParser`. For error codes not listed here, see the `<libxml/xmlerror.h>` header file.



---

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)