NSXMLParserErrorDomain错误41

时间:2011-08-31 07:25:57

标签: objective-c xml nsxmlparser

我正在解析一个给出错误的xml文件

NSXMLParserErrorDomain错误41

有人会告诉我这个错误的原因是什么以及如何解决这个错误。

先谢谢。

XML文件就像

 <GetOpenQuestionsForUserResult>
 <Questions Count="2">
 <Question id="44">
 <Question Title>test for mitesh</Question Title>
 <Question Description>please respond from mobile</Question Description>
 <Creator Name>test@abc.com</Creator Name>
 <Creation Date>08/29/2011</Creation Date>
 <Respondent Names>test@abc.com</Respondent Names>
 <Anonymous Answers>Yes</Anonymous Answers>
 <Expiry Date>08/29/2011</Expiry Date>
 </Question>

 <Question id="45">
 <Question Title>test for mitesh 2</Question Title>
 <Question Description>second question for mitesh mobile</Question Description>
 <Creator Name>test1@abc.com</Creator Name>
 <Creation Date>08/29/2011</Creation Date>
 <Respondent Names>test1@abc.com</Respondent Names>
 <Anonymous Answers>Yes</Anonymous Answers>
 <Expiry Date>08/31/2011</Expiry Date>
 </Question>
 </Questions>
 </GetOpenQuestionsForUserResult>
 </GetOpenQuestionsForUserResponse>

请告诉我如何解析此xml文件

我做了以下代码解析

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict {


if([elementName isEqualToString:@"Question"]) 
{
    [QuesIdArray addObject: [attributeDict objectForKey:@"id"]];
}
   else if([elementName isEqualToString:@"Question Title"]) 
{

}
else if([elementName isEqualToString:@"Question Description"]) 
{

}
else if([elementName isEqualToString:@"Creator Name"]) 
{

}
else if([elementName isEqualToString:@"Creation Date"]) 
{

}
else if([elementName isEqualToString:@"Respondent Names"]) 
{

}
else if([elementName isEqualToString:@"Anonymous Answers"]) 
{

}

else if([elementName isEqualToString:@"Expiry Date"]) 
{

}

  }

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

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


NSLog(@"Processing Value: %@", currentElementValue);


  }



   - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

if(currentElementValue) 
    currentElementValue = [currentElementValue stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

if([elementName isEqualToString:@"Error"]) 
{

    //appDelegate.ErrorString = [ [ NSString alloc ] initWithString: currentElementValue ] ;
}

else if([elementName isEqualToString:@"Question Title"]) 
{

    [QuesTitleArray addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Question Description"]) 
{

    [QuesTextArray addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Creator Name"]) 
{
    [QuesCreatorArray addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Creation Date"]) 
{
    [QuesCreationDateArray addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Respondent Names"]) 
{
    [ResNamesArray addObject:currentElementValue];
}
else if([elementName isEqualToString:@"Anonymous Answers"]) 
{
    [QuesAnswerArray addObject:currentElementValue];
}

else if([elementName isEqualToString:@"Expiry Date"]) 
{

    [QuesExpiryDateArray addObject:currentElementValue];
}

//[currentElementValue release];

currentElementValue = nil;
}

任何人都可以告诉我在这里做了什么错误。 它给出了一个错误:NSXMLParserErrorDomain错误41

3 个答案:

答案 0 :(得分:1)

来自NSXMLParser reference - 41错误代码值为NSXMLParserAttributeHasNoValueError

问题是您的xml格式不正确,因为xml标记名称不能包含空格。例如

<Question Title>...

此处标记名称为“Question”,“Title”被解释为属性名称,没有值。您应该更正您的xml以修复它(即删除标记名称中的空格)。

答案 1 :(得分:0)

尝试不为标签名称提供空格, 说,你给了<Question Title>test for mitesh</Question Title>

制作

<QuestionTitle>test for mitesh</QuestionTitle>

其他标签也可以尝试

答案 2 :(得分:0)

NSXMLParserErrorDomain error 41表示属性没有值。因为您没有正确命名元素,所以最终会得到一个没有值的属性。