Class

NSXMLElement

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.

Overview

Subclassing Notes

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.

Methods to Override

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.

NSXMLElement implements isEqual: 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:.

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.

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 because 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.

Symbols

Initializing NSXMLElement Objects

- initWithName:

Returns an NSXMLElement object initialized with the specified name.

- initWithName:stringValue:

Returns an NSXMLElement object initialized with a specified name and a single text-node child containing a specified value.

- initWithXMLString:error:

Returns an NSXMLElement object created from a specified string containing XML markup.

- initWithName:URI:

Returns an NSXMLElement object initialized with the specified name and URI.

Obtaining Child Elements

- elementsForName:

Returns the child element nodes (as NSXMLElement objects) of the receiver that have a specified name.

- elementsForLocalName:URI:

Returns the child element nodes (as NSXMLElement objects) of the receiver that are matched with the specified local name and URI.

Manipulating Child Elements

- addChild:

Adds a child node at the end of the receiver’s current list of children.

- insertChild:atIndex:

Inserts a new child node at a specified location in the receiver’s list of child nodes.

- insertChildren:atIndex:

Inserts an array of child nodes at a specified location in the receiver’s list of children.

- removeChildAtIndex:

Removes the child node of the receiver identified by a given index.

- replaceChildAtIndex:withNode:

Replaces a child node at a specified location with another child node.

- setChildren:

Sets all child nodes of the receiver at once, replacing any existing children.

- normalizeAdjacentTextNodesPreservingCDATA:

Coalesces adjacent text nodes of the receiver that you have explicitly added, optionally including CDATA sections.

Handling Attributes

- addAttribute:

Adds an attribute node to the receiver.

- attributeForName:

Returns the attribute node of the receiver with the specified name.

- attributeForLocalName:URI:

Returns the attribute node of the receiver that is identified by a local name and URI.

attributes

Sets all attributes of the receiver at once, replacing any existing attribute nodes.

- removeAttributeForName:

Removes an attribute node identified by name.

- setAttributesWithDictionary:

Sets the attributes of the receiver based on the key-value pairs specified in the passed dictionary.

- setAttributesAsDictionary:

Sets the attributes of the receiver based on the key-value pairs specified in the passed-in dictionary.

Deprecated

Handling Namespaces

- addNamespace:

Adds a namespace node to the receiver.

namespaces

Sets all of the namespace nodes of the receiver at once, replacing any existing namespace nodes.

- namespaceForPrefix:

Returns the namespace node with a specified prefix.

- removeNamespaceForPrefix:

Removes a namespace node that is identified by a given prefix.

- resolveNamespaceForName:

Returns the namespace node with the prefix matching the given qualified name.

- resolvePrefixForNamespaceURI:

Returns the prefix associated with the specified URI.

Relationships

Inherits From