First, Introduction
JSON parsing:
Pass the JSON ([{...},{...} ...]) Breaks down into multiple sections ({...}) that contain the contents of an entity instance, and in one section, finds and assigns values (regular lookup implementations) to the instance through the runtime (<objc/runtime.h>) to the instances ({...}- > An instance of the entity), traversing all the bars ({...}) the XML is converted to Nsmutablearray (similar to:list<class>).
XML parsing:
The XML is decomposed into multiple sections of XML that contain the contents of the entity instance through Gdataxml (or regular), through runtime (<objc/runtime.h>) in a section Find and assign values to all attribute values of an entity (regular lookup implementation) to the instance, traversing all the subsections to convert the XML to Nsmutablearray (similar to:list<class>) .
Array parsing:
Splicing XML to convert XML to Nsmutablearray (similar to:list<string>) via Gdataxml(the whole process can also be implemented by regular) .
3 parsing methods have been written to a common class ( globalapplication) in
Note that there are 2 header files to add in the implementation method
#import <objc/runtime.h> #import "GDataXMLNode.h"
Instructions for use:
User is an entity class //XML---Nsmutablearray (list<class>) nsmutablearray *retval = [globalapplication json Toarray:xml Class:User.class]; XML--Nsmutablearray (list<class>) nsmutablearray *retval = [Globalapplication xmltoarray:xml class: User.class rowrootname:@ "Row"]; XML--Nsmutablearray (list<string>) nsmutablearray *retval = [Globalapplication xmltoarray:xml];
Second, the Code
1, GlobalApplication.h
globalapplication.h//webservciebysoap////Created by fengs on 14-11-19.//Copyright (c) 2014 fengs. All rights reserved.//#import <Foundation/Foundation.h> @interface globalapplication:nsobject#pragma Mark-# Pragma mark-converts XML (array) to Nsmutablearray (list<string>)/** * converts XML (array) to Nsmutablearray * @param XML <STRING> ;fs</string> <string>fs</string> ... * @return Nsmutablearray (list<string>) */+ (nsmutable array*) Xmltoarray: (nsstring*) XML; #pragma mark-#pragma mark-Convert standard XML (entity) to Nsmutablearray (list<class>)/** * Convert standard XML (entity) to Nsmutablearray * @param xml: <data xmlns= "" > <row><userid>ff0f0704</userid>& Lt Username>fs</username></row> <row><userid>ff0f0704</userid><username>fs </UserName></row> ... </data> * @param class:user * @param rowrootname:row * @return Nsmutablearray (list<class>) */+ (Nsmutablearray*) Xmltoarray: (nsstring*) XML class: (Class) class rowrootname:rowrootname, #pragma mark-#pragma mark-the standard JSON (entity) Convert to Nsmutablearray (list<class>)/** * Convert standard XML (entity) to Nsmutablearray * @param xml: [{"UserID": "ff0f0704", "UserName ":" FS "}, {" UserID ":" ff0f0704 "," UserName ":" FS "},...] * @param class:user * @return Nsmutablearray (list<class>) */+ (nsmutablearray*) Jsontoarray: (nsstring*) JSON class :(Class) class; @end
2, GLOBALAPPLICATION.M
globalapplication.m//webservciebysoap////Created by fengs on 14-11-19.//Copyright (c) 2014 fengs. All rights reserved.//#import "GlobalApplication.h" #import <objc/runtime.h> #import "GDataXMLNode.h" @ Implementation Globalapplication#pragma mark-#pragma mark-converts XML (array) to Nsmutablearray (list<string>)/** * XML ( Array) converted to Nsmutablearray * @param xml <string>fs</string> <string>fs</string> ... * @retur n Nsmutablearray (list<string>) */+ (nsmutablearray*) Xmltoarray: (nsstring*) xml{Nsmutablearray *retVal = [[[NS] Mutablearray alloc] init] autorelease]; XML = [NSString stringwithformat:@ "<data>%@</data>", XML]; Gdataxmldocument *root = [[[Gdataxmldocument alloc] initwithxmlstring:xml options:0 Error:nil] autorelease]; Gdataxmlelement *rootele = [Root rootelement]; for (int i=0; I <[rootele ChildCount]; i++) {Gdataxmlnode *item = [Rootele childatindex:i]; [RetVal AddObject:item.stringValUE]; } return retVal;} #pragma mark-#pragma mark-converts standard XML (entity) to Nsmutablearray (list<class>)/** * Converts standard XML (entity) to Nsmutablearray * @param XML: <data xmlns= "" > <row><userid>ff0f0704</userid><username>fs</username>< ;/row> <row><UserID>ff0f0704</UserID><UserName>fs</UserName></row> ... </data> * @param class:user * @param rowrootname:row * @return Nsmutablearray (list<class>) */+ ( nsmutablearray*) Xmltoarray: (nsstring*) XML class: (Class) class rowrootname:rowrootname{Nsmutablearray *retval = [[[N] Smutablearray alloc] init] autorelease]; Gdataxmldocument *root = [[[Gdataxmldocument alloc] initwithxmlstring:xml options:0 Error:nil] autorelease]; Gdataxmlelement *rootele = [Root rootelement]; Nsarray *rows = [Rootele elementsforname:rowrootname]; For (gdataxmlelement *row in rows) {id object = [[Class alloc] init]; Object = [Self INitwithxmlstring:row. XMLString Object:object]; [RetVal Addobject:object]; [Object release]; } return retVal;} /** * Assign a passed entity assignment * @param XML (ignoring entity attribute case differences): <row><userid>ff0f0704</userid><username>fs</ username></row> * @param class:user @property Username,userid; * @return class */+ (ID) initwithxmlstring: (nsstring*) XML object: (ID) object{unsigned int outcount, I; objc_property_t *properties = Class_copypropertylist ([object class], &outcount); for (i = 0; i<outcount; i++) {objc_property_t = properties[i]; Const char* Char_f = Property_getname (property); NSString *propertyname = [NSString stringwithutf8string:char_f]; NSString *value = [self setxmlproperty:xml propertyname:propertyname]; [Object Setvalue:value Forkey:propertyname]; } free (properties); return object;} /** * @param content (ignoring entity attribute case differences) via regular assignment of entities passed over *: <row><userid>ff0f0704</userid><username>fs</username></row> * @param propertyname:userid * @return NSString FF 0f0704 */+ (nsstring*) Setxmlproperty: (nsstring*) value PropertyName: (nsstring*) propertyname {NSString *retVal = @ ""; NSString *patternstring = [NSString stringwithformat:@ "(?<=<%@>) (. *) (?=</%@>)", PropertyName, PropertyName]; Caseinsensitive: Case-insensitive comparison nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern: Patternstring options:nsregularexpressioncaseinsensitive Error:nil]; if (regex) {Nstextcheckingresult *firstmatch = [Regex firstmatchinstring:value Options:nscaseinsensitivesearch ran Ge:nsmakerange (0, [value length])]; if (firstmatch) {retVal = [value substringWithRange:firstMatch.range]; }} return retVal;} #pragma mark-#pragma mark-Converts the standard JSON (entity) to Nsmutablearray (list<class>)/** * Converts the standard JSON (entity) to Nsmutablearray * @ param xml: [{"UserID": "ff0f0704", "UserName":"FS"}, {"UserID": "ff0f0704", "UserName": "FS"},...] * @param class:user * @return Nsmutablearray (list<class>) */+ (nsmutablearray*) Jsontoarray: (nsstring*) JSON class :(Class) class {Nsmutablearray *retval = [[[[Nsmutablearray alloc] init] autorelease]; NSString *patternstring = @ "\\{.*?\\}"; Nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern:patternstring options:0 Error:nil]; if (regex) {Nsarray *match = [Regex matchesinstring:json options:0 range:nsmakerange (0, [JSON length])]; if (match) {for (Nstextcheckingresult *result in match) {nsstring *jsonrow = [JSON substringwi ThRange:result.range]; ID object = [[Class alloc] init]; Object = [self Initwithjsonstring:jsonrow object:object]; [RetVal Addobject:object]; [Object release]; }}} return retVal;} /** * @param XML (ignoring entity case differences) will be passed over to the entity assignment *: {"UserID": "Ff0f0704 "," UserName ":" FS "} * @param class:user @property Username,userid; * @return class */+ (ID) initwithjsonstring: (nsstring*) JSON object: (ID) object{unsigned int outcount, I; objc_property_t *properties = Class_copypropertylist ([object class], &outcount); for (i = 0; i<outcount; i++) {objc_property_t = properties[i]; Const char* Char_f = Property_getname (property); NSString *propertyname = [NSString stringwithutf8string:char_f]; NSString *value = [self Setjsonproperty:json propertyname:propertyname]; [Object Setvalue:value Forkey:propertyname]; } free (properties); return object;} /** * @param content (ignoring entity case differences) through regular assignment of entities passed in: {"UserID": "ff0f0704", "UserName": "FS"} * @param propertyname:useri D * @return nsstring ff0f0704 */+ (nsstring*) Setjsonproperty: (nsstring*) value PropertyName: (nsstring*) PropertyName { NSString *retval = @ ""; NSString *patternstring = [NSString stringwithformat:@ "(? <=\ "%@\": \ ") [^\",]* ", PropertyName]; Caseinsensitive: Case-insensitive comparison nsregularexpression *regex = [nsregularexpression regularexpressionwithpattern: Patternstring options:nsregularexpressioncaseinsensitive Error:nil]; if (regex) {Nstextcheckingresult *firstmatch = [Regex firstmatchinstring:value Options:nscaseinsensitivesearch ran Ge:nsmakerange (0, [value length])]; if (firstmatch) {retVal = [value substringWithRange:firstMatch.range]; }} return retVal;} @end
iOS Xml,json, array parsing and conversion to Nsmutablearray (list<class>)