我正在尝试使用NSXML Parser从.NET WCF Web服务解析XML。 这是我的Web服务返回的XML:
<QuestionnaireXML xmlns="http://schemas.datacontract.org/2004/07/QuestionnaireDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<QuestionnaireName>Clinic Waiting Room Feedback</QuestionnaireName>
<Questions>
<Question>
<Answers>
<Answer>
<AnswerTitle>Windows</AnswerTitle>
</Answer>
<Answer>
<AnswerTitle>Mac</AnswerTitle>
</Answer>
</Answers>
<QuestionID>363</QuestionID>
<QuestionName>Windows or Mac?</QuestionName>
</Question>
</Questions>
</QuestionnaireXML>
我使用以下方法获取XML:
-(IBAction)getQuestionnaire
{
// Obtain questionnaire ID from input textfield
NSString *qid = questionnaireId.text;
// Begin creating URL string and contact web service
dataWebService = [NSMutableData data];
NSString *combinedRequest = [NSString stringWithFormat:@"http://www.osqar.co.uk/OsqarWCF/frank.svc/getQuestionnaireForID?id=%@", qid];
NSURL * myURL = [NSURL URLWithString:combinedRequest];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self];
[myConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
XMLParser *parseQuestionnaire = [[XMLParser alloc] init];
Questionnaire *myQuestionnaire = [parseQuestionnaire parseXML:responseString];
}
然后将XML解析为XMLParser类及其方法。像所以:
#import "XMLParser.h"
#import "Questionnaire.h"
@implementation XMLParser
@synthesize xmlParser;
-(Questionnaire *)parseXML:(NSString *)xml
{
userRet = [[Questionnaire alloc] init];
NSData *data = [xml dataUsingEncoding:NSUTF8StringEncoding];
self.xmlParser = [[NSXMLParser alloc] initWithData:data];
self.xmlParser.delegate = self;
if([self.xmlParser parse])
{
NSLog(@"The Xml is parsed.");
}
else
{
NSLog(@"Failed to parse the XML");
}
return userRet;
}
- (void) parser: (NSXMLParser *) parser parseErrorOccurred: (NSError *) parseError
{
NSLog(@"Error Parser:%@",[parseError localizedDescription]);
}
- (void) parser: (NSXMLParser *) parser didStartElement: (NSString *) elementName
namespaceURI: (NSString *) namespaceURI
qualifiedName: (NSString *) qName
attributes: (NSDictionary *) attributeDict
{
if ([elementName isEqualToString:@"QuestionnaireName"])
{
NSString *name = [attributeDict objectForKey:@"QuestionnaireName"];
NSLog(@"QuestionnaireName: %@", name);
userRet.QuestionnaireName = [attributeDict objectForKey:@"QuestionnaireName"];
}
}
我的问题是,显然找不到元素名称 QuestionnaireName 。我已经设置了一个断点并且代码步入 didStartElement 方法,但是然后逐步退出 if([elementName isEqualToString:@“QuestionnaireName”])语句。
从上面的XML示例中可以看出,Element QuestionnaireName显然存在。 我在这做错了什么?我错过了什么吗? 任何帮助将不胜感激。
答案 0 :(得分:4)
首先,您必须在方法 didstartelement 中设置currentelement = elementName
。
现在必须实施方法 foundcharacter 。
在该方法中,请检查以下内容:
if ([currentelement isEqualtoString:@"QuestionnaireName"])
{
NSLog(@"value %@",string);
}
然后你将获得价值“诊所候诊室反馈”