正如许多人建议的那样,您可以使用第三方库来做到这一点。有关如何使用 GDataXML 执行此操作的示例。
首先创建适合保存数据的自定义对象。识别节点。这里有两个节点
- 旅行
- 观点
为它们创建模型类。为了简单起见,我把它们都做成了字符串。理想情况下,最好包括时间NSDate
和坐标CLLocation
以及持续时间(以秒为单位)。
@interface Trip : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic, copy) NSString *start_address;
@property (nonatomic, copy) NSString *end_address;
@property (nonatomic, copy) NSString *start_time;
@property (nonatomic, copy) NSString *end_time;
@property (nonatomic, copy) NSString *start_gps;
@property (nonatomic, copy) NSString *end_gps;
@property (nonatomic, copy) NSString *duration;
@property (nonatomic, strong) NSArray *points;
- (GDataXMLElement *)XMLNode;
- (NSString *)XMLString;
.m 文件
@implementation Trip
- (GDataXMLElement *)XMLNode{
GDataXMLElement *tripNode = [GDataXMLElement elementWithName:@"Mytrip"];
GDataXMLElement *startTimeElement = [GDataXMLElement elementWithName:@"start_time" stringValue:self.start_time];
[tripNode addChild:startTimeElement];
GDataXMLElement *endTimeElement = [GDataXMLElement elementWithName:@"end_time" stringValue:self.end_time];
[tripNode addChild:endTimeElement];
GDataXMLElement *startAddressElement = [GDataXMLElement elementWithName:@"start_address" stringValue:self.start_address];
[tripNode addChild:startAddressElement];
GDataXMLElement *endAddressElement = [GDataXMLElement elementWithName:@"end_address" stringValue:self.end_address];
[tripNode addChild:endAddressElement];
GDataXMLElement *startGpsElement = [GDataXMLElement elementWithName:@"start_gps" stringValue:self.start_gps];
[tripNode addChild:startGpsElement];
GDataXMLElement *endGpsElement = [GDataXMLElement elementWithName:@"end_gps" stringValue:self.end_gps];
[tripNode addChild:endGpsElement];
GDataXMLElement *durationElement = [GDataXMLElement elementWithName:@"duration" stringValue:self.duration];
[tripNode addChild:durationElement];
GDataXMLElement *labelElement = [GDataXMLElement elementWithName:@"label" stringValue:self.label];
[tripNode addChild:labelElement];
GDataXMLElement *routeNode = [GDataXMLElement elementWithName:@"route"];
for (RoutePoint *point in self.points) {
GDataXMLElement *pointNode = [point XMLNode];
[routeNode addChild:pointNode];
}
[tripNode addChild:routeNode];
return tripNode;
}
- (NSString *)XMLString{
return [[self XMLNode] XMLString];
}
Route 点也一样
@interface RoutePoint : NSObject
@property (nonatomic, strong) NSString *gps;
@property (nonatomic, copy) NSString *speed;
- (GDataXMLElement *)XMLNode;
- (NSString *)XMLString;
.m 文件
@implementation RoutePoint
- (GDataXMLElement *)XMLNode{
GDataXMLElement *pointNode = [GDataXMLElement elementWithName:@"point"];
GDataXMLElement *gpsElement = [GDataXMLElement elementWithName:@"gps" stringValue:self.gps];
GDataXMLElement *speedElement = [GDataXMLElement elementWithName:@"speed" stringValue:self.speed];
[pointNode addChild:gpsElement];
[pointNode addChild:speedElement];
return pointNode;
}
- (NSString *)XMLString{
return [[self XMLNode] XMLString];
}
创建 Trip 的实例填充值
Trip *trip = [Trip new];
trip.start_time = @"06/06/2013 09:00:00 AM";
trip.end_time = @"06/06/2013 09:30:00 AM";
trip.start_address = @"12 NGO, Roseville, Mp";
trip.end_address = @"8 STE stree, Roseville, MP";
trip.start_gps = @"+40.689060,-74.044636";
trip.end_gps = @"+42.689060, -72.044636";
trip.duration = @"00:00:00:00";
trip.label = @"Roseville Trip";
RoutePoint *point = [RoutePoint new];
point.gps = @"+40.689060,-74.044636";
point.speed = @"1.2 Miles/Hr";
trip.points = @[point];
NSLog(@"%@",[trip XMLString]);