Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.4 and later. |
Companion guide | |
Declared in | NSXMLElement.h |
Related sample code |
Instances of the NSXMLElement
class represent element nodes in an XML tree structure. An NSXMLElement
object may have child nodes, specifically comment nodes, processing-instruction nodes, text nodes, and other NSXMLElement
nodes. It may also have attribute nodes and namespace nodes associated with it (however, namespace and attribute nodes are not considered children). Any attempt to add a NSXMLDocument
node, NSXMLDTD
node, namespace node, or attribute node as a child raises an exception. If you add a child node to an NSXMLElement
object and that child already has a parent, NSXMLElement
raises an exception; the child must be detached or copied first.
You can subclass NSXMLElement
if you want element nodes with more specialized attributes or behavior, for example, paragraph and font attributes that specify how the string value of the element should appear.
To subclass NSXMLElement
you need to override the primary initializer, initWithName:URI:
, 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 NSXMLElement
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. 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.
Note that you can safely set the root element of the XML document (using the NSXMLDocument
setRootElement:
method) to be an instance of your subclass since this method only checks to see if the added node is of an element kind (NSXMLElementKind
). These precautions do not apply, of course, if you are creating an XML tree programmatically.
– addChild:
– insertChild:atIndex:
– insertChildren:atIndex:
– removeChildAtIndex:
– replaceChildAtIndex:withNode:
– setChildren:
– normalizeAdjacentTextNodesPreservingCDATA:
– addAttribute:
– attributeForName:
– attributeForLocalName:URI:
– attributes
– removeAttributeForName:
– setAttributes:
– setAttributesAsDictionary:
– addNamespace:
– namespaces
– namespaceForPrefix:
– removeNamespaceForPrefix:
– resolveNamespaceForName:
– resolvePrefixForNamespaceURI:
– setNamespaces:
Adds an attribute node to the receiver.
- (void)addAttribute:(NSXMLNode *)anAttribute
An XML node object representing an attribute. If the receiver already has an attribute with the same name, anAttribute is not added.
The order of multiple attributes is preserved if the NSXMLPreserveAttributeOrder
option is specified when the element is created.
NSXMLElement.h
Adds a child node at the end of the receiver’s current list of children.
- (void)addChild:(NSXMLNode *)child
An XML node object to add to the receiver's children.
The new node has an index value that is one greater than the last of the current children.
NSXMLElement.h
Adds a namespace node to the receiver.
- (void)addNamespace:(NSXMLNode *)aNamespace
An XML node object of kind NSXMLNamespaceKind
. If the receiver already has a namespace with the same name, aNamespace is not added.
– namespaces
– namespaceForPrefix:
– removeNamespaceForPrefix:
– resolveNamespaceForName:
– resolvePrefixForNamespaceURI:
– setNamespaces:
NSXMLElement.h
Returns the attribute node of the receiver that is identified by a local name and URI.
- (NSXMLNode *)attributeForLocalName:(NSString *)localName URI:(NSString *)URI
A string specifying the local name of an attribute.
A sting identifying the URI associated with an attribute.
An XML node object representing a matching attribute or nil
if no such node was found.
NSXMLElement.h
Returns the attribute node of the receiver with the specified name.
- (NSXMLNode *)attributeForName:(NSString *)name
A string specifying the name of an attribute.
An XML node object representing a matching attribute or nil
if no such node was found.
If name is a qualified name, then this method invokes attributeForLocalName:URI:
with the URI parameter set to the URI associated with the prefix. Otherwise comparison is based on string equality of the qualified or non-qualified name.
NSXMLElement.h
Returns the receiver’s attributes
- (NSArray *)attributes
An array of NSXMLNode
objects of kind NSXMLAttributeKind
or nil
if the receiver has no attribute nodes.
NSXMLElement.h
Returns the child element nodes (as NSXMLElement
objects) of the receiver that are matched with the specified local name and URI.
- (NSArray *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI
A string specifying a local name of an element.
A string specifying a URI associated with an element.
An array of NSXMLElement
objects or nil if no matching children could be found.
NSXMLElement.h
Returns the child element nodes (as NSXMLElement
objects) of the receiver that have a specified name.
- (NSArray *)elementsForName:(NSString *)name
A string specifying the name of the child element nodes to find and return. If name is a qualified name, then this method invokes elementsForLocalName:URI:
with the URI parameter set to the URI associated with the prefix. Otherwise comparison is based on string equality of the qualified or non-qualified name.
An array of of NSXMLElement
objects or an empty array if no matching children can be found.
NSXMLElement.h
Returns an NSXMLElement
object initialized with the specified name.
- (id)initWithName:(NSString *)name
A string specifying the name of the element.
The initialized NSXMLElement
object or nil
if initialization did not succeed.
The XML string representation of this object is <
name></
name>
. This method invokes initWithName:URI:
with the URI parameter set to nil
.
NSXMLElement.h
Returns an NSXMLElement
object initialized with a specified name and a single text-node child containing a specified value.
- (id)initWithName:(NSString *)name stringValue:(NSString *)string
A string specifying the name of the element.
The string value of the receiver's text node.
The initialized NSXMLElement
object or nil
if initialization did not succeed.
The string representation of this object is <
name>
string</
name>
.
NSXMLElement.h
Returns an NSXMLElement
object initialized with the specified name and URI.
- (id)initWithName:(NSString *)name URI:(NSString *)URI
A string that specifies the qualified name of the element.
A string that specifies the namespace URI associated with the element.
The initialized NSXMLElement
object or nil
if initialization did not succeed.
You can look up the namespace prefix for this element node based on its URI using resolvePrefixForNamespaceURI:
. This method is the primary initializer for the NSXMLElement
class.
NSXMLElement.h
Returns an NSXMLElement
object created from a specified string containing XML markup.
- (id)initWithXMLString:(NSString *)string error:(NSError **)error
A string containing XML markup for an element.
On return, an NSError
object that describes any errors or warnings resulting from the parsing of the markup.
The initialized NSXMLElement
object or nil
if initialization did not succeed.
NSXMLElement.h
Inserts a new child node at a specified location in the receiver’s list of child nodes.
- (void)insertChild:(NSXMLNode *)child atIndex:(NSUInteger)index
An XML node object to be inserted as a child of the receiver.
An integer identifying a position in the receiver's list of children. An exception is raised if index is out of bounds.
Insertion of the node increments the indexes of sibling nodes after it.
– addChild:
– insertChildren:atIndex:
– removeChildAtIndex:
– replaceChildAtIndex:withNode:
– setChildren:
NSXMLElement.h
Inserts an array of child nodes at a specified location in the receiver’s list of children.
- (void)insertChildren:(NSArray *)children atIndex:(NSUInteger)index
An array of XML node objects to add as children of the receiver.
An integer identifying a position in the receiver's list of children. An exception is raised if index is out of bounds.
Insertion of the node increases the indexes of sibling nodes after it by the count of children.
– addChild:
– insertChild:atIndex:
– removeChildAtIndex:
– replaceChildAtIndex:withNode:
– setChildren:
NSXMLElement.h
Returns the namespace node with a specified prefix.
- (NSXMLNode *)namespaceForPrefix:(NSString *)name
A string specifying a namespace prefix.
An NSXMLNode
object of kind NSXMLNamespaceKind
or nil
if there is no namespace node with that prefix.
– addNamespace:
– namespaces
– removeNamespaceForPrefix:
– resolveNamespaceForName:
– resolvePrefixForNamespaceURI:
– setNamespaces:
NSXMLElement.h
Returns the namespace nodes of the receiver.
- (NSArray *)namespaces
An array of NSXMLNode
objects of kind NSXMLNamespaceKind
. Returns nil
if the receiver has no namespace nodes.
– addNamespace:
– namespaceForPrefix:
– removeNamespaceForPrefix:
– resolveNamespaceForName:
– resolvePrefixForNamespaceURI:
– setNamespaces:
NSXMLElement.h
Coalesces adjacent text nodes of the receiver that you have explicitly added, optionally including CDATA sections.
- (void)normalizeAdjacentTextNodesPreservingCDATA:(BOOL)preserve
YES
if CDATA sections are left alone as text nodes, NO
otherwise.
A text node with a value of an empty string is removed. When you process an input source of XML, adjacent text nodes are automatically normalized. You should invoke this method (with preserve as NO
) before using the NSXMLNode
methods objectsForXQuery:constants:error:
or nodesForXPath:error:
.
NSXMLElement.h
Removes an attribute node that is identified by its name.
- (void)removeAttributeForName:(NSString *)attrName
A string specifying the name of an attribute.
The removed XML node object is released.
NSXMLElement.h
Removes the child node of the receiver identified by a given index.
- (void)removeChildAtIndex:(NSUInteger)nodeIndex
An integer identifying the node in the receiver's list of children to remove. An exception is raised if index is out of bounds.
The XML node object is released upon removal. The indices of subsequent children are decremented by one.
NSXMLElement.h
Removes a namespace node that is identified by a given prefix.
- (void)removeNamespaceForPrefix:(NSString *)name
A string that is the prefix for a namespace.
The removed XML node object is removed.
NSXMLElement.h
Replaces a child node at a specified location with another child node.
- (void)replaceChildAtIndex:(NSUInteger)index withNode:(NSXMLNode *)node
An integer identifying a position in the receiver's list of children. An exception is raised if index is out of bounds.
An XML node object that will replace the current child.
The replaced XML node object is released upon removal.
NSXMLElement.h
Returns the namespace node with the prefix matching the given qualified name.
- (NSXMLNode *)resolveNamespaceForName:(NSString *)name
A string that is the qualified name for a namespace (a qualified name is prefix plus local name).
An NSXMLNode
object of kind NSXMLNamespaceKind
or nil
if there is no matching namespace node.
The method looks in the entire namespace chain for the prefix.
NSXMLElement.h
Returns the prefix associated with the specified URI.
- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI
A string identifying the URI associated with the namespace.
A string that is the matching prefix or nil
if it finds no matching prefix.
The method looks in the entire namespace chain for the URI.
NSXMLElement.h
Sets all attributes of the receiver at once, replacing any existing attribute nodes.
- (void)setAttributes:(NSArray *)attributes
An array of NSXMLNode
objects of kind NSXMLAttributeKind
. If there are attribute nodes with the same name, the first attribute with that name is used. Send this message with attributes as nil
to remove all attributes.
To set attributes in an element node using an NSDictionary
object as the input parameter, see setAttributesAsDictionary:
.
NSXMLElement.h
Sets the attributes of the receiver based on the key-value pairs specified in the passed-in dictionary.
- (void)setAttributesAsDictionary:(NSDictionary *)attributes
A dictionary of key-value pairs where the attribute name is the key and the object value of the attribute is the dictionary value.
The method uses these names and object values to create NSXMLNode
objects of kind NSXMLAttributeKind
. Existing attributes are removed.
NSXMLElement.h
Sets all child nodes of the receiver at once, replacing any existing children.
- (void)setChildren:(NSArray *)children
An array of NSXMLElement
objects or NSXMLNode
objects of kinds NSXMLElementKind
, NSXMLProcessingInstructionKind
, NSXMLTextKind
, or NSXMLCommentKind
.
Send this message with children as nil
to remove all child nodes.
– insertChild:atIndex:
– insertChildren:atIndex:
– removeChildAtIndex:
– replaceChildAtIndex:withNode:
– setChildren:
NSXMLElement.h
Sets all of the namespace nodes of the receiver at once, replacing any existing namespace nodes.
- (void)setNamespaces:(NSArray *)namespaces
An array of NSXMLNode
objects of kind NSXMLNamespaceKind
. If there are namespace nodes with the same prefix, the first attribute with that prefix is used. Send this message with namespaces as nil
to remove all namespace nodes.
– addNamespace:
– namespaces
– namespaceForPrefix:
– removeNamespaceForPrefix:
– resolveNamespaceForName:
– resolvePrefixForNamespaceURI:
NSXMLElement.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-27)