Class

NSXMLDocument

An XML document as internalized into a logical tree structure.

Declaration

@interface NSXMLDocument : NSXMLNode

Overview

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

The initializers of the NSXMLDocument 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 NSXMLDocument 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 NSXMLDocument as XML, XHTML, HTML, or plain text.

The NSXMLDocument 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, initWithData:options:error:, 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.

By default NSXMLDocument implements the NSObject 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 replacementClassForClass: 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

- initWithContentsOfURL:options:error:

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

- initWithData:options:error:

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

- initWithRootElement:

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

- initWithXMLString:options:error:

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

+ replacementClassForClass:

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

Managing Document Attributes

characterEncoding

Sets the character encoding of the receiver to encoding,

documentContentKind

Sets the kind of output content for the receiver.

DTD

Returns an NSXMLDTD object representing the internal DTD associated with the receiver.

- setDTD:

Sets the internal DTD to be associated with the receiver.

- isStandalone

Returns whether the receiver represents a standalone XML document—that is, one without an external DTD.

standalone

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

MIMEType

Returns the MIME type for the receiver.

- setMIMEType:

Sets the MIME type of the receiver.

- URI

Returns the URI identifying the source of this document.

- setURI:

Sets the URI identifying the source of this document.

version

Sets the version of the receiver’s XML.

Managing the Root Element

- rootElement

Returns the root element of the receiver.

- setRootElement:

Set the root element of the receiver.

Adding and Removing Child Nodes

- addChild:

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

- insertChild:atIndex:

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

- insertChildren:atIndex:

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

- removeChildAtIndex:

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

- replaceChildAtIndex:withNode:

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

- setChildren:

Sets the child nodes of the receiver.

Transforming a Document Using XSLT

- objectByApplyingXSLT:arguments:error:

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.

- objectByApplyingXSLTString:arguments:error:

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.

- objectByApplyingXSLTAtURL:arguments:error:

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 object containing plain text, RTF text, and so on.

Writing a Document as XML Data

XMLData

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

- XMLDataWithOptions:

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

Validating a Document

- validateAndReturnError:

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

Constants

Input and Output Options

Input and output options specifically intended for NSXMLDocument objects.

NSXMLDocumentContentKind

Type used to define the kind of document content.

Document Content Types

Define document types.

Initializers

Relationships

Inherits From

See Also

Tree-Based Processing

NSXMLDTD

A representation of a Document Type Definition.

NSXMLDTDNode

A representation of element, attribute-list, entity, and notation declarations in a Document Type Definition.

NSXMLElement

The element nodes in an XML tree structure.

NSXMLNode

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