解析NSXMLParser问题

时间:2012-02-01 13:26:19

标签: objective-c ios parsing nsxmlparser

我正在尝试解析来自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)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
    attributes:(NSDictionary *)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(@"Processing Element: %@", elementName);
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

    if(!currentElementValue) 
    {
        currentElementValue = [[NSMutableString alloc] initWithString:string];
    }
    else
    {
        [currentElementValue appendString:string];
    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)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:@"b:LastName"])
    {
        NSLog(@"found lastname");
    }

    else if([elementName isEqualToString:@"b:GroupName"])
    {
        NSLog(@"found groupname");
    }

    currentElementValue = nil;

}
@end

现在我只是想看看它是否找到了数据......然后我会保存它...... 但是在我的didEndElement中,它似乎找不到除

之外的任何东西
if([elementName isEqualToString:@"GetKidsResult"])

我的xml响应示例:

            <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>
                <b:LastName>XXXXXX</b:LastName>
                <b:Photo>XXXXXXXX</b:Photo>
                <b:GroupName>XXXXX</b:GroupName>
            </a:Value>
        </a:KeyValueOfintKidf4KEWLbb>

任何想法为什么它不是其他元素? 我之前从未做过Web服务,所以解释越简单越好......

当我NSLog元素时,我可以看到它正确地通过xml文件

NSLog(@"Processing 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: b:LastName
Processing Element: b:Photo
Processing Element: b:GroupName

1 个答案:

答案 0 :(得分:3)

didEndElement中,尝试更改此部分:

if([elementName isEqualToString:@"GetKidsResult"])

    NSLog(@"end of xml");
    return;  

为:

if([elementName isEqualToString:@"GetKidsResult"])
{
    NSLog(@"end of xml");
    return;  
}

如果不在块if之后放置这两个语句,return每次都会执行。