我試圖使用NSXML解析器.NET WCF web服務解析XML。 這裏是XML其中我的網絡的服務回報:NSXML解析器找不到元素名稱
<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方法但隨後步驟出來的如果([的ElementName isEqualToString:@「QuestionnaireName」])語句。
你可以從XML樣品上方看到的是,Element QuestionnaireName明顯存在。 我在這裏做錯了什麼?我錯過了什麼嗎? 任何幫助將不勝感激。
無關,但在didStartElement,如果元素是「QuestionnaireName」,你再想找一個同名的屬性。根據示例XML,元素QuestionnaireName沒有屬性(如QuestionnaireXML有兩個屬性:的xmlns和xmlns:1)。您已實現其他委託方法像foundCharacters和didEndElement? – Anna 2012-02-21 03:18:54