MantleXMLExtension 1.2.3

MantleXMLExtension 1.2.3

TestsTested
LangLanguage Obj-CObjective C
License NOASSERTION
ReleasedLast Release Apr 2022

Maintained by soranoba.



  • By
  • soranoba

MantleXMLExtension

Build Status Carthage compatible Version License Platform

MantleXMLExtension support mutual conversion between Model object and XML with Mantle.

Overview

Mantle support the Json, but doesn't support XML.

This application is an extension for handling xml with Mantle.

  • Support these
    • Attributes
    • Child nodes, Nested child nodes, Array of child nodes
    • Customizable order of child nodes
    • Customizable XML declaration
    • Customizable transformer

What is Mantle ?

Model framework for Cocoa and Cocoa Touch

Installation

CocoaPods

To install it, simply add the following line to your Podfile:

pod 'MantleXMLExtension'

Carthage

To install it, simply add the following line to your Cartfile:

github "soranoba/MantleXMLExtension"

How to use functions of MantleXMLExtension

Conversion between Model object and XML

// XML to Model
id<MTLModel> model = [MXEXmlAdapter modelOfClass:model.class
                                     fromXmlData:xmlData
                                           error:&error];

// Model to XML
NSData* data = [MXEXmlAdapter xmlDataFromModel:model error:&error];

Model definition

Just add some to MTLModel for MXEXmlAdapter.

#pragma mark - MXEXmlSerializing

+ (NSDictionary<NSString*, id>* _Nonnull)xmlKeyPathsByPropertyKey
{
    return @{ @"status" : MXEXmlAttribute(@"", @"status"),
              @"userCount" : @"summary.count",
              @"users" : MXEXmlArray(@"", MXEXmlChildNode(@"user")) };
}

+ (NSString* _Nonnull)xmlRootElementName
{
    return @"response";
}

Path type

MXEXmlAdapter support 5 types of paths.

MXEXmlValuePath

For example:

<parent>
  <child>value</child>
</parent>
@"parent.child"

If you get value, please use this. Value doesn't support MXEXmlSerializing object.

MXEAttributePath

For example:

<parent>
  <child key="value" />
</parent>
MXEXmlAttribute(@"parent.child", @"key")

If you get value of specified attribute, please use this.

MXEChildNodePath

For example:

<parent>
  <child>
    <id>1</id>
    <name>Alice</name>
  </child>
</parent>
MXEXmlChildNode(@"parent.child")

If you get nested MXEXmlSerializing object, please use this. This path only support MXEXmlSerializing object.

MXEArrayPath

For example:

<parent>
  <children>
    <child>...</child>
    <child>...</child>
    <child>...</child>
  </children>
</parent>
MXEXmlArray(@"parent.children", MXEXmlChildNode(@"child"))

If you get array of value, please use this. This path can be used in combination with other path.

If you use this, you MUST use MXEXmlAdapter # xmlNodeArrayTransformerWithModelClass:.

+ (NSValueTransformer* _Nonnull)childrenXmlTransformer
{
    return [MXEXmlAdapter xmlNodeArrayTransformerWithModelClass:ChildModel.class];
}

Multiple elements of XML

For example:

<parent>
  <element_a>....</element_a>
  <element_b title="...." />
</parent>
@[@"parent.element_a", MXEXmlAttribute(@"parent.element_b", @"title")]

It is used when you want to transfer multiple elements of XML to another model. Please notice that root element of XML does not change.

Transformer

You can use these transformer for MXEXmlSerializing object.

  • MXEXmlAdapter # xmlNodeArrayTransformerWithModelClass:
  • MXEXmlAdapter # xmlNodeTransformerWithModelClass:
  • MXEXmlAdapter # mappingDictionaryTransformerWithKeyPath:valuePath:

You can use these transformer for primitive type.

  • MXEXmlAdapter # numberTransformer
  • MXEXmlAdapter # boolTransformer

Other information

Please refer to documentation, unit tests and Mantle.

Contribute

Pull request is welcome =D

License

MIT License