字符串中的Xml响应,DidStartElement不调用NSXMLParser

时间:2012-09-10 06:42:49

标签: iphone objective-c xcode nsxmlparser

这里我在临时字符串中获取xml响应。我需要从该xml响应中获取一个标记值。

-(void) httpDataDidFinishLoadingWithData:(NSData *)theData
{
    m_activityLoaded=NO;
    temp=[[NSString alloc] initWithData:[dataLoader httpData]  encoding:NSUTF8StringEncoding];
    NSLog(@"TEMP IS TEMP %@", temp);
    parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:temp]];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    parser.delegate=self;
    [parser parse];
}

问题是在上面的解析器分配之后,DidStartElement甚至没有调用并准备解析。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
    if(![elementName isEqual:@"Result"])
        return;
    woeid = [attributeDict objectForKey:@"woeid"];
    NSLog(@"woeid   %@", woeid);
}

我的XML响应是

<?xml version="1.0" encoding="UTF-8"?>
<Body><woied></woied></Body>

请帮帮我这些家伙。在此先感谢

2 个答案:

答案 0 :(得分:0)

? [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:temp]]; ?

也许你应该用[[NSXMLParser alloc] initWithData:theData]创建解析器;

答案 1 :(得分:0)

你可以试试这个:

-(void) httpDataDidFinishLoadingWithData:(NSData *)theData
{
    m_activityLoaded=NO;
    temp=[[NSString alloc] initWithData:[dataLoader httpData]  encoding:NSUTF8StringEncoding];
    NSLog(@"TEMP IS TEMP %@", temp);
    parser=[[NSXMLParser alloc] initWithData:[temp dataUsingEncoding: NSUTF8StringEncoding];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    parser.delegate=self;
    [parser parse];
}

我认为这可能有用。您将错误的数据传递给解析器。