NSXML 解析器找不到元素名称

发布于 2025-01-07 15:45:47 字数 3681 浏览 4 评论 0原文

我正在尝试使用 NSXML 解析器从 .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 显然存在。 我在这里做错了什么?我是不是错过了什么? 任何帮助将不胜感激。

I am trying to parse XML from a .NET WCF Webservice using NSXML Parser.
Here is the XML which my web-service returns:

<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>

I obtain the XML using the following method:

-(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];  
}

And then parse the XML to the XMLParser class and it method. Like So:

#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"];
    }   
}

My problem here is that the element name QuestionnaireName is apparently not being found. I have placed a break point and the code steps into didStartElement method but then steps out of the if ([elementName isEqualToString:@"QuestionnaireName"]) statement.

As you can see from the XML sample above, the Element QuestionnaireName clearly exists.
What am I doing wrong here? Have I missed out anything?
Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-01-14 15:45:47

首先,您必须在方法 didstartelement 中设置 currentelement = elementName

现在必须实现foundcharacter方法。

在该方法中检查以下内容:

if ([currentelement isEqualtoString:@"QuestionnaireName"])
{    
    NSLog(@"value  %@",string);    
}

然后您将获得值“诊所候诊室反馈”

First you have to set currentelement = elementName in the method didstartelement.

Now have to implement the method foundcharacter.

In that method check the following:

if ([currentelement isEqualtoString:@"QuestionnaireName"])
{    
    NSLog(@"value  %@",string);    
}

then you will get value "Clinic Waiting Room Feedback"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文