在我正在制作的 iOS 程序中,我请求使用 SOAP 的类(class)列表。这是回复格式
<ListCoursesResponse>
<statusMessage xmlns="xxx">string</statusMessage>
<courseID xmlns="xxx">string</courseID>
<courseTitle xmlns="xxx">string</courseTitle>
</ListCoursesResponse>
<ListCoursesResponse>
<statusMessage xmlns="xxx">string</statusMessage>
<courseID xmlns="xxx">string</courseID>
<courseTitle xmlns="xxx">string</courseTitle>
</ListCoursesResponse>
我使用的方法是将每个单独的类(class)提取为数组中的一个元素,而不是逐步遍历每个类(class)并提取类(class) ID 之间的内容。但是,我不知道如何提取多个类(class),因为当我返回它时,它只显示第一道菜。如果我解释得不好,请告诉我,以便我可以尝试更好地解释。
我的问题是,处理 XML 响应中返回的大量类(class)(200 多个)的最佳方法是什么?
编辑:我正在使用的代码片段,仅返回 1 个 ID。
NSString *tag1Open = @"<ListCoursesResponse>";
NSString *tag1Close = @"</ListCoursesResponse>";
NSString *courseIDOpen = @"<courseID xmlns=\"http://drm.mediuscorp.com/\">";
NSString *courseIDClose = @"</courseID>";
result = @"";
NSArray *XMLarray1 = [XMLResult componentsSeparatedByString:tag1Open];
if ([XMLarray1 count] > 1) {
for (int i = 0; i < [XMLarray1 count]; i++) {
NSString *courseIDString = [[[XMLarray1 objectAtIndex:1]componentsSeparatedByString:tag1Close]objectAtIndex:0];
NSArray *courseID = [XMLResult componentsSeparatedByString:courseIDOpen];
if ([courseID count] > 1) {
for (int i = 0; i < [courseID count]; i++) {
courseIDString = [[[courseID objectAtIndex:1]componentsSeparatedByString:courseIDClose]objectAtIndex:0];
}
}
NSLog(@"Course ID: %@",courseIDString);
}
[persArray addObject:result];
for (int i = 0; i < [persArray count]; i++) {
NSLog(@"%@",[persArray objectAtIndex:i]);
}
}
最佳答案
与其尝试使用 NSString 方法自己解析 SOAP XML 响应,不如使用 XML 解析器来处理此任务。 iOS 有一个内置的 XML 解析器,名为 NSXMLParser这对你来说是可用的。 (还有许多可用的第三方 XML 解析器组件,包括 SAX 和 DOM 类型)
关于objective-c - iOS 中的 SOAP - 从 XML 中提取多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942076/