MantleXMLExtension 1.2.3

MantleXMLExtension 1.2.3

测试已测试
语言语言 Obj-CObjective C
许可证 NOASSERTION
发布最后发布2022年4月

soranoba 维护。



  • soranoba

MantleXMLExtension

Build Status Carthage compatible Version License Platform

MantleXMLExtension 支持使用 Mantle 实现模型对象与 XML 之间的相互转换。

概述

Mantle 支持处理 Json,但不支持 XML。

此应用是 Mantle 处理 XML 的扩展。

  • 支持以下内容
    • 属性
    • 子元素,嵌套的子元素,子元素数组
    • 可自定义子元素顺序
    • 可自定义 XML 声明
    • 可自定义转换器

什么是 Mantle?

Cocoa 和 Cocoa Touch 的模型框架

安装

CocoaPods

要安装它,只需将以下行添加到您的 Podfile 中

pod 'MantleXMLExtension'

Carthage

要安装它,只需将以下行添加到您的 Cartfile 中

github "soranoba/MantleXMLExtension"

如何使用 MantleXMLExtension 的函数

模型对象与 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];

模型定义

只需添加一些到 MTLModel 以支持 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";
}

路径类型

MXEXmlAdapter 支持 5 种路径类型。

MXEXmlValuePath

例如

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

如果您获取 value,请使用此方法。值不支持 MXEXmlSerializing 对象。

MXEAttributePath

例如

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

如果您要获取指定属性的值,请使用此方法。

MXEChildNodePath

例如

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

如果您获取嵌套的 MXEXmlSerializing 对象,请使用此方法。此路径只支持 MXEXmlSerializing 对象。

MXEArrayPath

例如

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

如果得到一个值数组,请使用此方法。此路径可以与其他路径结合使用。

使用时,务必使用 MXEXmlAdapter # xmlNodeArrayTransformerWithModelClass:

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

XML的多元素

例如

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

当您想将多个XML元素转换为另一个模型时使用。请注意,XML的根元素不会更改。

转换器

您可以使用这些转换器为MXEXmlSerializing对象。

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

您可以使用这些转换器用于原始类型。

  • MXEXmlAdapter # numberTransformer
  • MXEXmlAdapter # boolTransformer

其他信息

请参阅文档单元测试Mantle

贡献

欢迎提出拉取请求 =D

许可证

MIT许可证