Mac Developer Library

Developer

Foundation Framework Reference NSXMLElement Class Reference

Options
Deployment Target:

On This Page
Language:

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.

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.

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

    Declaration

    Swift

    func elementsForName(_ name: String) -> [NSXMLElement]

    Objective-C

    - (NSArray<NSXMLElement *> *)elementsForName:(NSString *)name

    Parameters

    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.

    Return Value

    An array of of NSXMLElement objects or an empty array if no matching children can be found.

    Availability

    Available in OS X v10.4 and later.

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

    Declaration

    Swift

    func elementsForLocalName(_ localName: String, URI URI: String?) -> [NSXMLElement]

    Objective-C

    - (NSArray<NSXMLElement *> *)elementsForLocalName:(NSString *)localName URI:(NSString *)URI

    Parameters

    localName

    A string specifying a local name of an element.

    URI

    A string specifying a URI associated with an element.

    Return Value

    An array of NSXMLElement objects or an empty array if no matching children could be found.

    Availability

    Available in OS X v10.4 and later.