Format Code Now
/
ConvertXML to Objective-C Next: XML to Python ›

XML to Objective-C Class Generator

CV-84

Paste an XML document and press Convert to get model classes with initWithDictionary:. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

XML

Objective-C

How to use it

  1. Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
  2. Pick a class prefix such as XX, or none. Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
  3. Press Convert, then copy the result or download it as a file.
  4. Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary]. Feed it the JSON form of your XML, not the XML itself.

How the classes are built

Each object becomes an NSObject subclass with properties, an initWithDictionary: initializer and a dictionary method that writes it back. Strings are NSString, arrays are NSArray with lightweight generics, nested objects are their own classes, and numbers and booleans use NSNumber when a key is null or missing in some items. Whole numbers use NSInteger, or long long beyond 32 bits, decimals use double and booleans use BOOL. Names that clash with C words, NSObject methods or ARC naming rules are changed, so id becomes idValue and newName becomes theNewName; a class prefix can be added.

How the XML becomes classes

Comments, processing instructions and the DOCTYPE are dropped, and namespace prefixes stay in the name. The XML is read into JSON first, using the same rules as the XML to JSON converter, and the code is built from that JSON. The root element becomes the single top-level key, so the first type holds one member named after it. An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. In Objective-C, @id becomes a property named idValue, read from the dictionary key @"@id".

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay NSString; convert them with NSDateFormatter if you need NSDate values. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.

Questions people ask

How do I convert XML to an Objective-C class?

Paste the XML into the input box, choose your options and press Convert to get the classes. Parse with NSJSONSerialization, then call [[Root alloc] initWithDictionary:dictionary]. The classes follow the JSON form of the XML. The conversion happens in your browser.

How do I load JSON into the generated Objective-C classes?

Parse the data with NSJSONSerialization into an NSDictionary, then call initWithDictionary: on the first class. Each value is checked with isKindOfClass: first, so a value of the wrong type is skipped and leaves its property empty. This applies to the JSON form of your XML.

Why is an Objective-C property named idValue or theNewName?

Some names clash with C words, NSObject methods or ARC naming rules. Keys such as id, class and description get a Value suffix, and names starting with new, copy, init or alloc get a the prefix. The original key is still used when reading and writing. This applies to the JSON form of your XML.

What does the class prefix option do in the Objective-C output?

Objective-C has no namespaces, so apps often prefix class names. Pick XX, API or App and every generated class name gets it, for example XXOrder. The default adds no prefix. This applies to the JSON form of your XML.

Why is an element a list in one XML sample and a single value in another?

XML does not say how many times an element may repeat. An element becomes a list only when it appears more than once under the same parent in your sample. To get a list type, paste a sample where the element appears at least twice. The Objective-C classes are written from that converted data.

Does it read the XML declaration, comments or namespaces?

The declaration, comments, processing instructions and DOCTYPE are read and then dropped. Namespace prefixes stay as part of the element name, and namespaces are not resolved. Custom entities are an error; only the five built-in ones and numeric character references are decoded. The Objective-C classes are written from that converted data.

About this tool

The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes an NSObject subclass with properties, an initWithDictionary: initializer that checks each value with isKindOfClass: before using it, and a dictionary method that writes the object back. The output holds a header section and an implementation section, separated by a comment line. Strings are NSString, arrays are NSArray with lightweight generics, nested objects are their own classes, and numbers use NSInteger, long long, double or BOOL, with NSNumber for keys that are null or missing in some items. Names that clash with C, Objective-C or ARC naming rules get a suffix or prefix, for example id becomes idValue. Values with the wrong type, and list items that fail to convert, are skipped rather than raising an error. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.