| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/Foundation.framework |
| Availability | Available in Mac OS X v10.4 and later. |
| Companion guide | |
| Declared in | NSXMLDocument.h NSXMLNodeOptions.h |
| Related sample code |
An instance of NSXMLDocument represents an XML document as internalized into a logical tree structure. 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.
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:.
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.
– initWithContentsOfURL:options:error:
– initWithData:options:error:
– initWithRootElement:
– initWithXMLString:options:error:
+ replacementClassForClass:
– characterEncoding
– setCharacterEncoding:
– documentContentKind
– setDocumentContentKind:
– DTD
– setDTD:
– isStandalone
– setStandalone:
– MIMEType
– setMIMEType:
– URI
– setURI:
– version
– setVersion:
– addChild:
– insertChild:atIndex:
– insertChildren:atIndex:
– removeChildAtIndex:
– replaceChildAtIndex:withNode:
– setChildren:
– objectByApplyingXSLT:arguments:error:
– objectByApplyingXSLTString:arguments:error:
– objectByApplyingXSLTAtURL:arguments:error:
Overridden by subclasses to substitute a custom class for an NSXML class that the parser uses to create node instances.
+ (Class)replacementClassForClass:(Class)class
A Class object identifying an NSXML class that is to be replaced by your custom class.
The substituted class.
For example, if you have a custom subclass of NSXMLElement that you want to be used in place of NSXMLElement, you would make the following override:
+ (Class)replacementClassForClass:(Class)currentClass { |
if ( currentClass == [NSXMLElement class] ) { |
return [MyCustomElementClass class]; |
} |
} |
This method is invoked before a document is parsed. The substituted class must be a subclass of NSXMLNode, NSXMLDocument, NSXMLElement, NSXMLDTD, or NSXMLDTDNode.
NSXMLDocument.hAdds a child node after the last of the receiver’s existing children.
- (void)addChild:(NSXMLNode *)child
The NSXMLNode object to be added.
NSXMLDocument.hReturns the character encoding used for the XML.
- (NSString *)characterEncoding
The character encoding used for the XML, or nil if no encoding is specified.
Typically the encoding is specified in the XML declaration of a document that is processed, but it can be set at any time. If the specified encoding does not match the actual encoding, parsing of the document may fail.
NSXMLDocument.hReturns the kind of document content for output.
- (NSXMLDocumentContentKind)documentContentKind
Most of the differences among content kind have to do with the handling of content-less tags such as <br>. The valid NSXMLDocumentContentKind constants are NSXMLDocumentXMLKind, NSXMLDocumentXHTMLKind, NSXMLDocumentHTMLKind, and NSXMLDocumentTextKind.
NSXMLDocument.hReturns an NSXMLDTD object representing the internal DTD associated with the receiver.
- (NSXMLDTD *)DTD
An NSXMLDTD object representing the internal DTD associated with the receiver or nil if no DTD has been associated.
NSXMLDocument.hInitializes and returns an NSXMLDocument object created from the XML or HTML contents of a URL-referenced source
- (id)initWithContentsOfURL:(NSURL *)url options:(NSUInteger)mask error:(NSError **)error
An NSURL object specifying a URL source.
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.
An error object that, on return, identifies any parsing errors and warnings or connection problems.
An initialized NSXMLDocument object, or nil if initialization fails because of parsing errors or other reasons.
NSXMLDocument.hInitializes and returns an NSXMLDocument object created from an NSData object.
- (id)initWithData:(NSData *)data options:(NSUInteger)mask error:(NSError **)error
A data object with XML content.
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.
An error object that, on return, identifies any parsing errors and warnings or connection problems.
An initialized NSXMLDocument object, or nil if initialization fails because of parsing errors or other reasons.
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 however, respect the xmlns:space="preserve" attribute when it attempts to tidy the XML.
NSXMLDocument.h Returns an NSXMLDocument object initialized with a single child, the root element.
- (id)initWithRootElement:(NSXMLElement *)root
An NSXMLElement object representing an XML element.
An initialized NSXMLDocument object, or nil if initialization fails for any reason.
– initWithContentsOfURL:options:error:– initWithData:options:error:– initWithXMLString:options:error:NSXMLDocument.hInitializes and returns an NSXMLDocument object created from a string containing XML markup text.
- (id)initWithXMLString:(NSString *)string options:(NSUInteger)mask error:(NSError **)error
A string object containing XML markup text.
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.
An error object that, on return, identifies any parsing errors and warnings or connection problems.
An initialized NSXMLDocument object, or nil if initialization fails because of parsing errors or other reasons.
The encoding of the document is set to UTF-8.
NSXMLDocument.hInserts a node object at specified position in the receiver’s array of children.
- (void)insertChild:(NSXMLNode *)child atIndex:(NSUInteger)index
The NSXMLNode object to be inserted. The added node must be an NSXMLNode object representing a comment, processing instruction, or the root element.
An integer specifying the index of the children array to insert child. The indexes of children after the new child are incremented. If index is less than zero or greater than the number of children, an out-of-bounds exception is raised.
NSXMLDocument.hInserts an array of children at a specified position in the receiver’s array of children.
- (void)insertChildren:(NSArray *)children atIndex:(NSUInteger)index
An array of NSXMLNode objects representing comments, processing instructions, or the root element.
An integer identifying the location in the receiver's children array for insertion. The indexes of children after the new child are increased by [children count]. If index is less than zero or greater than the number of children, an out-of-bounds exception is raised.
NSXMLDocument.hReturns whether the receiver represents a standalone XML document—that is, one without an external DTD.
- (BOOL)isStandalone
YES if the receiver represents a standalone XML document, NO if the “standalone” declaration was not present in the original document and hasn’t been set since.
NSXMLDocument.hReturns the MIME type for the receiver.
- (NSString *)MIMEType
The MIME type for the receiver (for example, “text/xml”).
MIME types are assigned by IANA (see http://www.iana.org/assignments/media-types/index.html).
NSXMLDocument.hApplies 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.
- (id)objectByApplyingXSLT:(NSData *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error
A data object containing the XSLT pattern rules and templates.
A dictionary containing NSString key-value pairs that are passed as runtime parameters to the XSLT processor. Pass in nil if you have no parameters to pass.
Note: Several XML websites discuss XSLT parameters, including O'Reilly Media’s http://www.xml.com.
If an error occurs, indirectly returns an NSError object encapsulating error or warning messages generated by XSLT processing.
Depending on intended output, the method returns an NSXMLDocument object or an NSData data containing transformed XML or HTML markup. If the message is supposed to create plain text or RTF, then an NSData object is returned, otherwise an XML document object. The method returns nil if XSLT processing did not succeed.
NSXMLDocument.hApplies 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.
- (id)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)arguments error:(NSError **)error
An NSURL object specifying a valid URL.
A dictionary containing NSString key-value pairs that are passed as runtime parameters to the XSLT processor. Pass in nil if you have no parameters to pass.
Note: Several XML websites discuss XSLT parameters, including O'Reilly Media’s http://www.xml.com.
If an error occurs, indirectly returns an NSError object encapsulating error or warning messages generated by XSLT processing or from an attempt to connect to a website identified by the URL.
Depending on intended output, the returns an NSXMLDocument object or an NSData data containing transformed XML or HTML markup. If the message is supposed to create plain text or RTF, then an NSData object is returned, otherwise an XML document object. The method returns nil if XSLT processing did not succeed.
NSXMLDocument.hApplies the XSLT pattern rules and templates (specified as a string) to the receiver and returns a document object containing transformed XML or HTML markup.
- (id)objectByApplyingXSLTString:(NSString *)xsltarguments:(NSDictionary *)argumentserror:(NSError **)error
A string object containing the XSLT pattern rules and templates.
A dictionary containing NSString key-value pairs that are passed as runtime parameters to the XSLT processor. Pass in nil if you have no parameters to pass.
Note: Several XML websites discuss XSLT parameters, including O'Reilly Media’s http://www.xml.com.
If an error occurs, indirectly returns an NSError object encapsulating error or warning messages generated by XSLT processing.
Depending on intended output, the method returns an NSXMLDocument object or an NSData data containing transformed XML or HTML markup. If the message is supposed to create plain text or RTF, then an NSData object is returned, otherwise an XML document object. The method returns nil if XSLT processing did not succeed.
NSXMLDocument.hRemoves the child node of the receiver located at a specified position in its array of children.
- (void)removeChildAtIndex:(NSUInteger)index
An integer identifying the position of an child in the receiver's array. If index is less than zero or greater than the number of children minus one, an out-of-bounds exception is raised.
Subsequent children have their indexes decreased by one. The removed NSXMLNode object is autoreleased.
NSXMLDocument.hReplaces the child node of the receiver located at a specified position in its array of children with another node.
- (void)replaceChildAtIndex:(NSUInteger)index withNode:(NSXMLNode *)node
An integer identifying a position in the receiver's array of children. If index is less than zero or greater than the number of children minus one, an out-of-bounds exception is raised.
An NSXMLNode object to replace the one at index; it must represent a comment, a processing instruction, or the root element.
The removed NSXMLNode object is autoreleased.
NSXMLDocument.hReturns the root element of the receiver.
- (NSXMLElement *)rootElement
The root element of the receiver.
NSXMLDocument.hSets the character encoding of the receiver to encoding,
- (void)setCharacterEncoding:(NSString *)encoding
A string that specifies an encoding; it must match the name of an IANA character set. See http://www.iana.org/assignments/character-sets for a list of valid encoding specifiers.
Typically the encoding is specified in the XML declaration of a document that is processed, but it can be set at any time. If the specified encoding does not match the actual encoding, parsing of the document might fail.
NSXMLDocument.hSets the child nodes of the receiver.
- (void)setChildren:(NSArray *)children
An array of NSXMLNode objects. Each of these objects must represent comments, processing instructions, or the root element; otherwise, an exception is raised. Pass in nil to remove all children.
NSXMLDocument.hSets the kind of output content for the receiver.
- (void)setDocumentContentKind:(NSXMLDocumentContentKind)kind
An enum constant identifying a kind of document content. The valid NSXMLDocumentContentKind constants are NSXMLDocumentXMLKind, NSXMLDocumentXHTMLKind, NSXMLDocumentHTMLKind, and NSXMLDocumentTextKind.
Most of the differences among document-content kind have to do with the handling of content-less tags such as <br>.
NSXMLDocument.hSets the internal DTD to be associated with the receiver.
- (void)setDTD:(NSXMLDTD *)documentTypeDeclaration
An NSXMLDTD object representing the internal DTD to be associated with the receiver.
When the receiver is written out, this document type declaration appears in the output, just after the XML declaration.
NSXMLDocument.hSets the MIME type of the receiver.
- (void)setMIMEType:(NSString *)MIMEType
A string object identifying a MIME type, for example, “text/xml”. MIME types are assigned by IANA (see http://www.iana.org/assignments/media-types/index.html).
NSXMLDocument.hSet the root element of the receiver.
- (void)setRootElement:(NSXMLNode *)root
An NSXMLNode object that is to be the root element.
As a side effect, this method removes all other children, including NSXMLNode objects representing comments and processing-instructions.
NSXMLDocument.hSets a Boolean value that specifies whether the receiver represents a standalone XML document.
- (void)setStandalone:(BOOL)standalone
YES if the receiver represents a standalone XML document, NO otherwise.
A standalone document does not have an external DTD associated with it.
NSXMLDocument.hSets the URI identifying the source of this document.
- (void)setURI:(NSString *)URI
A string object representing a URI source, or nil to remove the current URI.
This attribute is automatically set when the receiver is initialized using initWithContentsOfURL:options:error:.
Sets the version of the receiver’s XML.
- (void)setVersion:(NSString *)version
A string object identifying the version of the XML.
Currently, the version should be either “1.0 “or “1.1”.
NSXMLDocument.hReturns the URI identifying the source of this document.
- (NSString *)URI
The URI identifying the source of this document or nil if this attribute has not been set.
Validates the document against the governing schema and returns whether the document conforms to the schema.
- (BOOL)validateAndReturnError:(NSError **)error
If validation fails, on return contains an NSError object describing the reason or reasons for failure.
YES if the validation operation succeeded, otherwise NO.
The constants indicating the kind of validation errors are emitted by the underlying parser; see NSXMLParser.h for most of these constants. If the schema is defined with a DTD, this method uses the NSXMLDTD object set for the receiver for validation. If the schema is based on XML Schema, the method uses the URL specified as the value of the xsi:schemaLocation attribute of the root element.
You can validate an XML document when it is first processed by specifying the NSXMLDocumentValidate option when you initialize an NSXMLDocument object with the initWithContentsOfURL:options:error:, initWithData:options:error:, or initWithXMLString:options:error: methods.
NSXMLDocument.hReturns the version of the receiver’s XML.
- (NSString *)version
The version of the receiver’s XML or nil if the version has not be set.
NSXMLDocument.hReturns the XML string representation of the receiver—that is, the entire document—encapsulated in a data object.
- (NSData *)XMLData
This method invokes XMLDataWithOptions: with an option of NSXMLNodeOptionsNone. The encoding used is based on the value returned from characterEncoding or UTF-8 if no valid encoding is returned by that method.
NSXMLDocument.hReturns the XML string representation of the receiver—that is, the entire document—encapsulated in a data object.
- (NSData *)XMLDataWithOptions:(NSUInteger)options
One or more options (bit-OR'd if multiple) to affect the output of the document; see “Constants” for the valid output options.
The encoding used is based on the value returned from characterEncoding.
NSXMLDocument.hInput and output options specifically intended for NSXMLDocument objects.
NSXMLDocumentTidyHTML = 1 << 9, NSXMLDocumentTidyXML = 1 << 10, NSXMLDocumentValidate = 1 << 13, NSXMLDocumentXInclude = 1 << 16, NSXMLDocumentIncludeContentTypeDeclaration = 1 << 18,
NSXMLDocumentTidyHTMLFormats HTML into valid XHTML during processing of the document.
When tidying, NSXMLDocument adds a line break before the close tag of a block-level element (<p>, <div>, <h1>, and so on); it also makes the string value of <br> or <hr> a line break. These operations make the string value of the HTML <body> more readable. After using this option, avoid outputting the document as anything other than the default kind, NSXMLDocumentXHTMLKind.
(Input)
Available in Mac OS X v10.4 and later.
Declared in NSXMLNodeOptions.h.
NSXMLDocumentTidyXMLChanges malformed XML into valid XML during processing of the document.
It also eliminates “pretty-printing” formatting, such as leading tab characters. However, it respects the xmlns:space="preserve" attribute.
(Input)
Available in Mac OS X v10.4 and later.
Declared in NSXMLNodeOptions.h.
NSXMLDocumentValidateValidates this document against its DTD (internal or external) or XML Schema.
(Input)
Available in Mac OS X v10.4 and later.
Declared in NSXMLNodeOptions.h.
NSXMLDocumentXIncludeReplaces all XInclude nodes in the document with the nodes referred to.
XInclude allows clients to include parts of another XML document within a document.
(Input)
Available in Mac OS X v10.4 and later.
Declared in NSXMLNodeOptions.h.
NSXMLDocumentIncludeContentTypeDeclarationIncludes a content type declaration for HTML or XHTML in the output of the document.
(Output)
Available in Mac OS X v10.4 and later.
Declared in NSXMLNodeOptions.h.
Because NSXMLDocument is a subclass of NSXMLNode, you can also use the relevant input and output options described in “Constants” in the NSXMLNode class reference. You can specify input options in the NSXMLDocument methods initWithContentsOfURL:options:error:, initWithData:options:error:, initWithXMLString:options:error:. The XMLDataWithOptions: method takes output options.
NSXMLNodeOptions.hType used to define the kind of document content.
typedef NSUInteger NSXMLDocumentContentKind;
For possible values, see “Document Content Types.”
NSXMLDocument.hDefine document types.
enum {
NSXMLDocumentXMLKind = 0,
NSXMLDocumentXHTMLKind,
NSXMLDocumentHTMLKind,
NSXMLDocumentTextKind
};
NSXMLDocumentXMLKindThe default type of document content type, which is XML.
Available in Mac OS X v10.4 and later.
Declared in NSXMLDocument.h.
NSXMLDocumentXHTMLKindThe document output is XHTML.
This is set automatically if the NSXMLDocumentTidyHTML option is set and NSXML detects HTML.
Available in Mac OS X v10.4 and later.
Declared in NSXMLDocument.h.
NSXMLDocumentHTMLKindOutputs empty tags in HTML without a close tag, such as <br>.
Available in Mac OS X v10.4 and later.
Declared in NSXMLDocument.h.
NSXMLDocumentTextKindOutputs the string value of the document by extracting the string values from all text nodes.
Available in Mac OS X v10.4 and later.
Declared in NSXMLDocument.h.
You specify one of the NSXMLDocumentContentKind constants in setDocumentContentKind: to indicate the kind of content required for document output.
NSXMLDocument.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-27)