{"id":20200188,"url":"https://github.com/mxabc/lbxmlmodel","last_synced_at":"2025-10-18T21:49:18.611Z","repository":{"id":56918164,"uuid":"330336717","full_name":"MxABC/LBXMLModel","owner":"MxABC","description":"xml convert to jsonmodel(XML parsing), jsonmodel convert to xml(xml writer)","archived":false,"fork":false,"pushed_at":"2021-03-15T02:10:41.000Z","size":128,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T18:35:04.733Z","etag":null,"topics":["jsonmodel","model","xml","xmlmodel"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MxABC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-17T07:22:11.000Z","updated_at":"2022-09-09T02:28:57.000Z","dependencies_parsed_at":"2022-08-21T02:50:38.577Z","dependency_job_id":null,"html_url":"https://github.com/MxABC/LBXMLModel","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MxABC%2FLBXMLModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MxABC%2FLBXMLModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MxABC%2FLBXMLModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MxABC%2FLBXMLModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MxABC","download_url":"https://codeload.github.com/MxABC/LBXMLModel/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248208565,"owners_count":21065202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jsonmodel","model","xml","xmlmodel"],"created_at":"2024-11-14T04:42:20.350Z","updated_at":"2025-10-18T21:49:18.528Z","avatar_url":"https://github.com/MxABC.png","language":"Objective-C","readme":"# LBXMLModel\nxml 与model相互转换，达到类似YYModel使用效果，\n基于[XMLReader](https://github.com/amarcadet/XMLReader)、[XMLWriter](https://github.com/ahmyi/XMLWriter)、[YYModel](https://github.com/ibireme/YYModel)修改而成\n\n\n\n## 安装\n- cocoapods安装\n\n```\npod 'LBXMLModel'\n```\n\n- 手动安装\n\n将`LBXMLModel`文件夹copy到工程\n\n## 调用\n\n包含头文件 `NSObject+LBXMLModel.h`\n\nxml-\u003emodel\n\n```\nNSString *path = [[NSBundle mainBundle] pathForResource:@\"test\" ofType:@\"xml\"];\nNSData *dataXML = [NSData dataWithContentsOfFile:path];\n//xml -\u003emodel\nRootModel *model = [RootModel jsonModelWithXML:dataXML];\n```\n\nmodel-\u003exml\n\n```\nRootModel *rootModel = [[RootModel alloc]init];\n    //TODO:   初始化model值\nNSData *xmlData  = [rootModel jsonModelToXMLData:YES];\nNSString* strXML = [[NSString alloc]initWithData:xmlData encoding:NSUTF8StringEncoding];\nNSLog(@\"%@\",strXML);\n```\n\n\n# model定义注意事项\n\n## 常用xml报文格式\nxml数据只是标签内容，没有标签属性，那么直接按照YYModel使用注意事项即可\n\n如类似如下xml报文，各个标签没有属性(大部分情况都是如此)，model定义只要按照YYModel要求即可\n\n```\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003e\n\u003croot\u003e\n    \u003cRESPONSE_CODE\u003e200\u003c/RESPONSE_CODE\u003e\n    \u003cRESPONSE_MSG\u003e上传成功\u003c/RESPONSE_MSG\u003e\n    \u003cNODE\u003e\n        \u003cPAGEID\u003epage1\u003cPAGEID/\u003e\n        \u003cFILE_NAME\u003eFILE_NAME_123.jpg\u003cFILE_NAME/\u003e\n    \u003c/NODE\u003e\n    \u003cTITLE\u003etitl1\u003c/TITLE\u003e\n    \u003cTITLE\u003etitl2\u003c/TITLE\u003e\n\u003c/root\u003e\n```\n\n对应model定义\n\n```\n@interface NODEModel : NSObject\n@property (nonatomic, copy) NSString *PAGEID;\n@property (nonatomic, copy) NSString *FILE_NAME;\n@end\n\n@interface ResModel : NSObject\n@property (nonatomic, assign) NSInteger RESPONSE_CODE;\n@property (nonatomic, copy) NSString *RESPONSE_MSG;\n\n//有可能是数组的，均写成数组形式\n@property (nonatomic, strong) NSArray\u003cNODEModel*\u003e *NODE;\n@property (nonatomic, strong) NSArray\u003cNSString*\u003e *TITLE;\n@end\n\n@interface RootModel : NSObject\n@property (nonatomic, strong) ResModel *root;\n@end\n```\n\n```\n@implementation NODEModel\n@end\n\n@implementation ResModel\n\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"NODE\":[NODEModel class]\n            };\n}\n@end\n\n@implementation RootModel\n\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"root\" : [ResModel class]\n            };\n}\n@end\n```\n\n\n## 如果标签包含属性\n\n```\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" ?\u003e\n\u003croot\u003e\n    \u003cNODE\u003e\n        \u003cSUBNODE\u003esubnode\u003c/SUBNODE\u003e\n        \u003cPAGE PAGEID=\"11-22-33-44-66\" FILE_NAME=\"filename3.jpg\"/\u003e\n        \u003cPAGE PAGEID=\"11-22-33-44-77\" FILE_NAME=\"filename.xml\"\u003e\u003c/PAGE\u003e\n    \u003c/NODE\u003e\n    \u003cRESPONSE_MSG\u003e上传成功\u003c/RESPONSE_MSG\u003e\n    \u003cRESPONSE_CODE\u003e200\u003c/RESPONSE_CODE\u003e\n    \u003cPAGE PAGEID=\"11-22-33-44\" FILE_NAME=\"filename1.doc\"/\u003e\n    \u003cPAGE PAGEID=\"11-22-33-44-55\" FILE_NAME=\"filename2.pdf\"\u003e\u003c/PAGE\u003e\n    \u003cTITLE\u003etitl1\u003c/TITLE\u003e\n    \u003cTITLE\u003etitl2\u003c/TITLE\u003e\n    \u003cTagARRAY\u003e\n        \u003cTagSubARRAY subTitle=\"st1\"\u003esubArray1\u003c/TagSubARRAY\u003e\n    \u003c/TagARRAY\u003e\n    \u003cTagARRAY\u003e\n        \u003cTagSubARRAY\u003esubArray3\u003c/TagSubARRAY\u003e\n        \u003cTagSubARRAY\u003esubArray4\u003c/TagSubARRAY\u003e\n    \u003c/TagARRAY\u003e\n\u003c/root\u003e\n\n\n1、如果xml报文只是从服务器接收到用来解析(xml-\u003ejsonmodel)\n\n1)、有标签属性，且没有标签内容(大部分情况都是如此)，如上面的xml报文中的PAGE标签，那么定义  \nModel和json报文定义model没有区别\n\n2)、如果有标签属性且有标签内容，如上图的TagSubARRAY,包含属性 subTitle，且有内容   \n  subArray1，那么model需要定义字段为`NSString *tag_content_text`来表示标签内容，  \n  也可以通过YYModel提供的mapper方法自定义名称，如下面方法修改为名称text\n\n+ (NSDictionary *)modelCustomPropertyMapper {\n    return @{@\"text\" : @\"tag_content_text\",\n             };\n}\n\n2、如果jsonmodel需要打包成xml数据(jsonmodel-\u003ejson)\n1)、有标签属性，且没有标签内容(大部分情况都是如此)，如上面的xml报文中的PAGE标签，定义Model  \n安装普通model定义外，model需要定义类方法,返回对应的属性字段\n+ (NSArray*)modelContainerAttributePropertys\n{\n    return @[@\"subTitle\"];\n}\n\n2)、有标签属性，且包含标签内容  如上面xml的TagSubARRAY,包含属性 subTitle，且有内容   \nsubArray1需要额外增加`NSArray *xml_attribute_set`，并返回对应属性的名字数组 ，可  \n参考下面的model定义\n标签内容参数名称定义为`NSString *tag_content_text`来表示标签内容，也可以通过YYModel  \n提供的mapper方法自定义名称，如下面方法修改为名称text\n\n+ (NSDictionary *)modelCustomPropertyMapper {\n    return @{@\"text\" : @\"tag_content_text\",\n             };\n}\n\n```\n\n\n\n如上面报文对应的model\n\n\n```\n//头文件\n@interface PAGEModel : NSObject\n@property (nonatomic, copy) NSString *PAGEID;\n@property (nonatomic, copy) NSString *FILE_NAME;\n@end\n\n@interface NODEModel : NSObject\n@property (nonatomic, copy) NSString *SUBNODE;\n@property (nonatomic, strong) NSArray\u003cPAGEModel*\u003e *PAGE;\n@end\n\n//既包含属性，还有标签内容\n@interface TagSubARRAYModel : NSObject\n@property (nonatomic, copy) NSString *subTitle;\n@property (nonatomic, copy) NSString *text;//标签值\n\n@end\n@interface TagARRAYModel : NSObject\n@property (nonatomic, strong) NSArray\u003cTagSubARRAYModel*\u003e *TagSubARRAY;\n@end\n\n@interface ResModel : NSObject\n@property (nonatomic, assign) NSInteger RESPONSE_CODE;\n@property (nonatomic, copy) NSString *RESPONSE_MSG;\n\n//有可能是数组的，均写成数组形式\n@property (nonatomic, strong) NSArray\u003cPAGEModel*\u003e *PAGE;\n@property (nonatomic, strong) NSArray\u003cNODEModel*\u003e *NODE;\n@property (nonatomic, strong) NSArray\u003cNSString*\u003e *TITLE;\n\n@property (nonatomic, strong) NSArray\u003cTagARRAYModel*\u003e *TagARRAY;\n\n@end\n\n@interface RootModel : NSObject\n@property (nonatomic, strong) ResModel *root;\n@end\n```\n\n```\n@implementation PAGEModel\n\n//如果只是 xml-\u003ejsonmodel，该类方法可以不用实现\n+ (NSArray*)modelContainerAttributePropertys\n{\n    return @[@\"PAGEID\",@\"FILE_NAME\"];\n}\n@end\n\n@implementation NODEModel\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"PAGE\" : [PAGEModel class]\n            };\n}\n@end\n\n@implementation TagSubARRAYModel\n\n//如果只是 xml-\u003ejsonmodel，该类方法可以不用实现\n+ (NSArray*)modelContainerAttributePropertys\n{\n    return @[@\"subTitle\"];\n}\n//标签内容 字段为tag_content_text，如果想其他名称，这里mapper\n+ (NSDictionary *)modelCustomPropertyMapper {\n    return @{@\"text\" : @\"tag_content_text\"};\n}\n@end\n\n@implementation TagARRAYModel\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"TagSubARRAY\" : [TagSubARRAYModel class]\n            };\n}\n@end\n\n@implementation ResModel\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"PAGE\" : [PAGEModel class],@\"NODE\":[NODEModel class],@\"TagARRAY\":[TagARRAYModel class]\n            };\n}\n@end\n\n@implementation RootModel\n+ (NSDictionary *)modelContainerPropertyGenericClass {\n    return @{@\"root\" : [ResModel class]\n            };\n}\n@end\n```\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxabc%2Flbxmlmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmxabc%2Flbxmlmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmxabc%2Flbxmlmodel/lists"}