<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/XMLDocument/replacementClass(for:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSXMLDocument(cm)replacementClassForClass:"
  },
  "title" : "replacementClass(for:)"
}
-->

# replacementClass(for:)

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

```
class func replacementClass(for cls: AnyClass) -> AnyClass
```

## Parameters

`cls`

A `Class` object identifying an NSXML class that is to be replaced by your custom class.

## Return Value

The substituted class.

## Discussion

For example, if you have a custom subclass of [`XMLElement`](/documentation/Foundation/XMLElement) that you want to be used in place of `NSXMLElement`, you would make the following override:

```objc
+ (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 [`XMLNode`](/documentation/Foundation/XMLNode), `NSXMLDocument`, `NSXMLElement`, [`XMLDTD`](/documentation/Foundation/XMLDTD), or [`XMLDTDNode`](/documentation/Foundation/XMLDTDNode).

## See Also

[`setRootElement(_:)`](/documentation/Foundation/XMLDocument/setRootElement(_:))

Set the root element of the receiver.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)