简体   繁体   English

无法使用NSXML Parser进行解析

[英]Unable To Parse Using NSXML Parser

I have the following XML file : 我有以下XML文件:

<?xml version="1.0"? encoding="UTF-8"?>
<api>
        <count count="55" />
        <spa>
            <opt>aa</opt>
            <opt>bb</opt>
            <opt>cc</opt>

        </spa>
</api>

M using the following lines of code : M使用以下代码行:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"space.xml"];

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];

//Initialize the delegate.
XMLParser *theParser = [[XMLParser alloc] initXMLParser];

//Set delegate
[xmlParser setDelegate:theParser];

//Start parsing the XML file.
BOOL success = [xmlParser parse];

if(success)
    NSLog(@"No Errors");
else
    NSLog(@"Error Error Error!!!");

However, m getting the output as "error error error" in gdb. 但是,在gdb中输出为“错误错误错误”。 I am new to Objective C and unable to get through the error. 我是Objective C的新手,无法解决错误。 Can someone please help ?? 有人可以帮忙吗? Thanks. 谢谢。

// Try to use NSXMLParser //尝试使用NSXMLParser

NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL URLWithString:@"YourURL"]];
[parser setDelegate:self];
[parser parse];

// Below are the delegates which will get you the Data //以下是为您提供数据的代表

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

  {    
            if([elementName isEqualToString:@"spa"]){
           got = YES; //got is a BOOL and here we have encountere start tag i.e <spa>
     }
  }

    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
     {
          if(got)
          {
                 NSLog(@"the Data is = %@",string);
          }
     }

    -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
     {
         if([elementName isEqualToString:@"spa"])
        {
           got = NO; //Here we have encountered the end tag </spa>
        }

     }

Have you checked if ' path ' or ' data ' are nil? 你检查过' path '或' data '是否为零?

Also, if 'parse' didn't succeed, you can use the method ' parserError ' to get an NSError object that will hold more information about the problem. 此外,如果'parse'没有成功,您可以使用方法' parserError '来获取一个NSError对象,该对象将包含有关该问题的更多信息。 From NSXMLParser class reference: 从NSXMLParser类引用:

parserError parserError
Returns an NSError object from which you can obtain information about a parsing error. 返回一个NSError对象,您可以从中获取有关解析错误的信息。

 - (NSError *)parserError 

Discussion 讨论
You may invoke this method after a parsing operation abnormally terminates to determine the cause of error. 在解析操作异常终止以确定错误原因后,您可以调用此方法。

Availability 可用性
Available in iOS 2.0 and later. 适用于iOS 2.0及更高版本。

After this, you should be able to call the method ' localizedDescription ' on your NSError* object to get more information about the problem. 在此之后,您应该能够在NSError *对象上调用方法“ localizedDescription ”以获取有关该问题的更多信息。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM