我正在尝试解析来自 Web 服务器的响应。
我的解析器如下:
#import "XMLParser.h"
#import "AppDelegate.h"
#import "Child.h"
@implementation XMLParser
- (XMLParser *) initXMLParser {
self = [super init];
if(self)
{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
return self;
}
- (void)parserNSXMLParser *)parser didStartElementNSString *)elementName
namespaceURINSString *)namespaceURI qualifiedNameNSString *)qualifiedName
attributesNSDictionary *)attributeDict
{
if([elementName isEqualToString"GetKidsResult"])
{
// initialize the array
if(!appDelegate.children)
{
appDelegate.children = [[NSMutableArray alloc] init];
}
}
else if([elementName isEqualToString"a:KeyValueOfintKidf4KEWLbb"])
{
if(!aChild)
{
//Initialize the child.
aChild = [[Child alloc] init];
}
}
NSLog(@"rocessing Element: %@", elementName);
}
- (void)parserNSXMLParser *)parser foundCharactersNSString *)string {
if(!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
}
else
{
[currentElementValue appendString:string];
}
}
-(void)parserNSXMLParser *)parser didEndElementNSString *)elementName namespaceURINSString *)namespaceURI qualifiedName:(NSString *)qName
{
//NSLog(@"El name: %@", elementName);
if([elementName isEqualToString"GetKidsResult"])
NSLog(@"end of xml");
return;
if([elementName isEqualToString"a:KeyValueOfintKidf4KEWLbb"])
{
NSLog(@"Found end of child");
[appDelegate.children addObject:aChild];
aChild = nil;
}
else if([elementName isEqualToString"a:Key"])
{
NSLog(@"Found key");
}
else if([elementName isEqualToString"b:CheckedIn"])
{
NSLog(@"Found checkedIn");
}
else if([elementName isEqualToString"b:FirstName"])
{
NSLog(@"Found firstname");
}
else if([elementName isEqualToString"b:Gender"])
{
NSLog(@"found gender");
}
else if([elementName isEqualToString"b:Id"])
{
NSLog(@"found id");
}
else if([elementName isEqualToString"b:IsOnTour"])
{
NSLog(@"found isontour");
}
else if([elementName isEqualToString:@"bastName"])
{
NSLog(@"found lastname");
}
else if([elementName isEqualToString:@"b:GroupName"])
{
NSLog(@"found groupname");
}
currentElementValue = nil;
}
@end
if([elementName isEqualToString:@"GetKidsResult"])
<a:KeyValueOfintKidf4KEWLbb>
<a:Key>XXXXXXXX</a:Key>
<a:Value xmlns:b="http://schemas.datacontract.org/2004/07/DayCare.DB.DTO">
<b:CheckedIn>false</b:CheckedIn>
<b:FirstName>XXXXXXX</b:FirstName>
<b:Gender>Male</b:Gender>
<b:Id>XXXXXXXX</b:Id>
<b:IsOnTour>false</b:IsOnTour>
<bastName>XXXXXX</bastName>
<bhoto>XXXXXXXX</bhoto>
<b:GroupName>XXXXX</b:GroupName>
</a:Value>
</a:KeyValueOfintKidf4KEWLbb>
NSLog(@"rocessing Element: %@", elementName);
Processing Element: a:KeyValueOfintKidf4KEWLbb
Processing Element: a:Key
Processing Element: a:Value
Processing Element: b:CheckedIn
Processing Element: b:FirstName
Processing Element: b:Gender
Processing Element: b:Id
Processing Element: b:IsOnTour
Processing Element: bastName
Processing Element: bhoto
Processing Element: b:GroupName
在 didEndElement
,尝试改变这部分:
if([elementName isEqualToString:@"GetKidsResult"])
NSLog(@"end of xml");
return;
if([elementName isEqualToString:@"GetKidsResult"])
{
NSLog(@"end of xml");
return;
}
if
之后在一个街区中,return
每次都被执行。
关于objective-c - 使用 NSXMLParser 问题解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096563/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |