FormatterHQ

XML to Objective-C Class Generator

Generate Objective-C model classes from XML: @property declarations for attributes, child elements and element text, with NSArray for repeated elements.

Input XML
Output OBJECTIVEC
Ready.

How to use the XML to Objective-C

  1. Paste your XML document into the Input editor, or use Sample, Upload file or Load URL, or press Sample to see an example.
  2. Leave Top-level class name empty to name the root type after the root element, or type your own name.
  3. Tick Types only (no helpers) for interfaces with empty implementations, or leave it off for JSON-dictionary helper methods.
  4. Press Generate (it also updates as you edit), then use Copy or Download.

XML to Objective-C model classes

Older iOS and macOS code often consumes XML: RSS and Atom feeds, plist-like configuration, and SOAP or custom XML APIs. This XML to Objective-C generator turns a sample document into model classes with typed @property declarations, using the QT class prefix and nullability annotations, so you have a clear target to fill while parsing.

Example

<playlist name="Road Trip">
  <track id="1" duration="215">Highway Song</track>
  <track id="2" duration="187">Open Road</track>
</playlist>
@interface QTPlaylist : NSObject
@property (nonatomic, copy) NSArray<QTTrack *> *track;
@property (nonatomic, copy) NSString *name;
@end

@interface QTTrack : NSObject
@property (nonatomic, copy)   NSString *value;
@property (nonatomic, assign) NSInteger identifier;
@property (nonatomic, assign) NSInteger duration;
@end

Each <track> has attributes and text, so the class has properties for id and duration plus value for the song title. The id attribute is renamed identifier because id is a type in Objective-C.

Filling the models with NSXMLParser

Foundation's NSXMLParser is event-driven: it calls your delegate as it meets each element. A typical pattern keeps the object currently being built and a buffer for text:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)name
  namespaceURI:(NSString *)ns qualifiedName:(NSString *)qn attributes:(NSDictionary *)attrs {
    if ([name isEqualToString:@"track"]) {
        self.current = [QTTrack new];
        self.current.identifier = [attrs[@"id"] integerValue];
        self.current.duration = [attrs[@"duration"] integerValue];
        [self.text setString:@""];
    }
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    [self.text appendString:string];
}

In didEndElement you assign self.text to value and add the track to the playlist's array. Attribute values arrive as strings, so convert them with integerValue, doubleValue or boolValue.

Where XML still shows up on Apple platforms

Even in JSON-first apps, XML appears in RSS and Atom feeds, podcast feeds, OPML subscription lists, SOAP services in enterprise apps, and appcast feeds used by the Sparkle update framework on macOS. Generating models from a real sample of such a feed gives you a checked list of every element and attribute to handle in your parser delegate, which is easy to lose track of in a long didStartElement method.

Notes

  • The helper methods in full mode (fromJSONDictionary:) read dictionaries shaped like the tool's JSON view of your XML, not XML itself.
  • Namespace prefixes are dropped in property names; enable shouldProcessNamespaces on the parser if you need the namespace URIs.
  • Include two or more of each repeated element in the sample so it is typed as an NSArray.

Format the classes with the Objective-C Formatter. For Swift projects, XML to Swift produces structs instead. Check the XML with the XML Formatter. Generation runs in your browser.

Frequently asked questions

Does the generated Objective-C code parse XML?
No. It defines the model classes. Use NSXMLParser (or a library such as KissXML) to read the document and populate them.
Why is the id attribute called identifier?
id is a built-in Objective-C type, so the generator renames the property to identifier to avoid confusion.
Where does element text go?
When an element has attributes and text, the text is stored in a value property of that element’s class.
Can I change the QT prefix?
Yes. Replace QT with your project’s class prefix throughout the generated files.
Should I use NSXMLParser or NSXMLDocument?
NSXMLParser is a streaming parser available on iOS, macOS and every other Apple platform, and it uses little memory. NSXMLDocument builds a full tree with XPath support, but it exists only on macOS. For iOS apps, NSXMLParser or a third-party tree parser is the choice.
How do I make the classes nicer to use from Swift?
Wrap the interfaces in NS_ASSUME_NONNULL_BEGIN and NS_ASSUME_NONNULL_END, then mark properties that can be missing as nullable. Swift then imports them as non-optional or optional types instead of implicitly unwrapped ones.

Last updated September 26, 2026

Related tools

JSON to Objective-C Generate Objective-C classes with properties from a JSON sample instantly, with nested types detected automatically. XML to PHP Generate PHP classes with typed properties from a XML sample instantly, with nested types detected automatically. XML to Pike Generate Pike classes from a XML sample instantly, with nested types detected automatically. XML to Python Generate Python dataclasses with type hints from a XML sample instantly, with nested types detected automatically. XML to Ruby Generate Ruby Dry::Struct classes from a XML sample instantly, with nested types detected automatically. XML to Rust Generate Rust structs with Serde derives from a XML sample instantly, with nested types detected automatically. XML to Scala Generate Scala case classes from a XML sample instantly, with nested types detected automatically. XML to Swift Generate Swift Codable structs from a XML sample instantly, with nested types detected automatically. XML to TypeScript Generate TypeScript interfaces from a XML sample instantly, with nested types detected automatically. GraphQL to TypeScript Generate TypeScript interfaces, enums and union types from a GraphQL schema (SDL), with nullability and argument types. JSON Schema to TypeScript Turn JSON Schema into TypeScript interfaces and types with optional properties, enums, $ref definitions and JSDoc comments. JSON to C++ Generate C++ classes with nlohmann::json helpers from a JSON sample instantly, with nested types detected automatically.