-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSXmlJsonAnalyze.m
200 lines (180 loc) · 6.64 KB
/
FSXmlJsonAnalyze.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// FSXmlJsonAnalyze.m
// https://github.com/Ericfengshi/FSXmlJsonAnalyze
//
// Created by fengs on 14-11-19.
// Copyright (c) 2014年 fengs. All rights reserved.
//
#import "FSXmlJsonAnalyze.h"
#import <objc/runtime.h>
#import "GDataXMLNode.h"
@implementation FSXmlJsonAnalyze
#pragma mark -
#pragma mark - 将xml(数组)转换成NSMutableArray (List<String>)
/**
* 将xml(数组)转换成NSMutableArray
* @param xml:
<string>fs</string>
<string>fs</string>
...
* @return NSMutableArray (List<String>)
*/
+(NSMutableArray*)xmlToArray:(NSString*)xml{
NSMutableArray *retVal = [[[NSMutableArray 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 - 将标准的xml(实体)转换成NSMutableArray (List<class>)
/**
* 将标准的xml(实体)转换成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 @property userName,userID;
* @param rowRootName:
row
* @return NSMutableArray (List<User>)
*/
+(NSMutableArray*)xmlToArray:(NSString*)xml class:(Class)class rowRootName:rowRootName{
NSMutableArray *retVal = [[[NSMutableArray 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];
[retVal addObject:[self initWithXMLString:row.XMLString object:object]];
[object release];
}
return retVal;
}
/**
* 将传递过来的实体赋值
* @param xml(忽略实体属性大小写差异):
<row><UserID>ff0f0704</UserID><UserName>fs</UserName></row>
* @param object:
user @property userName,userID;
* @return user
*/
+(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 property = 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 value(忽略实体属性大小写差异):
<row><UserID>ff0f0704</UserID><UserName>fs</UserName></row>
* @param propertyName:
userID
* @return NSString
ff0f0704
*/
+(NSString*)setXMLProperty:(NSString*)value propertyName:(NSString*)propertyName {
NSString *retVal = @"";
NSString *patternString = [NSString stringWithFormat:@"(?<=<%@>)(.*)(?=</%@>)",propertyName,propertyName];
// CaseInsensitive:不区分大小写比较
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patternString options:NSRegularExpressionCaseInsensitive error:nil];
if (regex) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:value options:NSCaseInsensitiveSearch range:NSMakeRange(0, [value length])];
if (firstMatch) {
retVal = [value substringWithRange:firstMatch.range];
}
}
return retVal;
}
#pragma mark -
#pragma mark - 将标准的Json(实体)转换成NSMutableArray (List<class>)
/**
* 将标准的Json(实体)转换成NSMutableArray
* @param json:
[{"UserID":"ff0f0704","UserName":"fs"},
{"UserID":"ff0f0704","UserName":"fs"},...]
* @param class:
User @property userName,userID;
* @return NSMutableArray (List<User>)
*/
+(NSMutableArray*)jsonToArray:(NSString*)json class:(Class)class {
if (!json) {
return nil;
}
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 substringWithRange:result.range];
id object = [[class alloc] init];
[retVal addObject:[self initWithJsonString:jsonRow object:object]];
[object release];
}
}
}
return retVal;
}
/**
* 将传递过来的实体赋值
* @param json(忽略实体大小写差异):
{"UserID":"ff0f0704","UserName":"fs"}
* @param object:
user @property userName,userID;
* @return user
*/
+(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 property = 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 value(忽略实体大小写差异):
{"UserID":"ff0f0704","UserName":"fs"}
* @param propertyName:
userID
* @return NSString
ff0f0704
*/
+(NSString*)setJsonProperty:(NSString*)value propertyName:(NSString*)propertyName {
NSString *retVal = @"";
NSString *patternString = [NSString stringWithFormat:@"(?<=\"%@\":\")[^\",]*",propertyName];
// CaseInsensitive:不区分大小写比较
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:patternString options:NSRegularExpressionCaseInsensitive error:nil];
if (regex) {
NSTextCheckingResult *firstMatch = [regex firstMatchInString:value options:NSCaseInsensitiveSearch range:NSMakeRange(0, [value length])];
if (firstMatch) {
retVal = [value substringWithRange:firstMatch.range];
}
}
return retVal;
}
@end