---
title: "init(data:options:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/xmldocument/init(data:options:)"
---

# init(data:options:)

Initializes and returns an NSXMLDocument object created from an NSData object.

## Declaration

```swift
init(data: Data, options mask: XMLNode.Options = []) throws
```

## Parameters

- `data`: A data object with XML content.
- `mask`: A bit mask for input options. You can specify multiple options by bit-OR’ing them. See Constants for a list of valid input options.

## Return Value

Return Value An initialized NSXMLDocument object, or  nil if initialization fails because of parsing errors or other reasons.

## Discussion

Discussion This method is the designated initializer for the NSXMLDocument class. If you specify NSXMLDocumentTidyXML as one of the options, NSXMLDocument performs several clean-up operations on the document XML (such as removing leading tabs). It does respect the xml:space="preserve" attribute when it attempts to tidy the XML. note: In Swift, this API is imported as an initializer and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## See Also

### Initializing NSXMLDocument Objects

- [init(contentsOf:options:)](foundation/xmldocument/init(contentsof:options:).md)
- [init(rootElement:)](foundation/xmldocument/init(rootelement:).md)
- [init(xmlString:options:)](foundation/xmldocument/init(xmlstring:options:)-65m2r.md)
- [replacementClass(for:)](foundation/xmldocument/replacementclass(for:).md)
