NSXMLParser - 空值

时间:2015-07-20 11:48:32

标签: ios xml-parsing

我正在尝试使用NSXMLParser来首次从XML获取值。但我有一个问题。

在我的XML下面:

<Library>
    <Book>
        <ID>1</ID>
        <TITLE>Test</TITLE>
    </Book>
</Library>

和解析器代码:

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString                                 *)   elementName namespaceURI:(NSString *)namespaceURI qualifiedNam(NSString *)qName attributes:(NSDictionary *)attributeDict
{
  if([elementName isEqualToString:@"Book"])
  {
    NSLog(@"%@", [attributeDict valueForKey:@"ID"]);
  }
}

结果ID为空。你能帮帮我吗?

2 个答案:

答案 0 :(得分:2)

不幸的是,NSXMLParser无法正常工作。它按文档顺序遍历XML中的元素,因此在“Book”元素启动时,它还没有到达“ID”元素。如果XML看起来像

,您的代码工作
<Book Id="1">
    ...
</Book>

但我想你无法控制XML。那么你需要一个更复杂的解决方案。我相信以下内容可行:

添加这些实例变量

NSString *currentElement, *bookID, *bookTitle;
NSMutableString *elementValue;

实施以下方法:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString                                 *)   elementName namespaceURI:(NSString *)namespaceURI qualifiedNam(NSString *)qName attributes:(NSDictionary *)attributeDict {
    currentElement = elementName;
    elementValue = nil; 
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    // Append characters to element value
    if (!elementValue) elementValue = [NSMutableString stringWithCapacity:100];
    [elementValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName {
     if ([@"Id" isEqualToString:elementName]) {
          bookID = [elementValue copy];
     } else if ([@"Title" isEqualToString:elementName]) {
          bookTitle = [elementValue copy];
     } else if ([@"Book" isEqualToString:elementName]) {
          // end of Book element, do something with bookID and bookTitle
     }
}

答案 1 :(得分:1)

按照以下代码。您将理解。

在你的.h部分

//步骤1:添加委托类

     First of all you should add <NSXMLParserDelegate>

//第2步:创建必要的对象

     NSXMLParser *parser;
     NSMutableData *ReceviedData;
     NSMutableString *currentStringValue;

     NSMutableArray *arrayID;

在你的.m部分

 //Step 3 - Allocate your all  Arrays in your viewDidLoad method

     arrayId = [NSMutableArray alloc]init];


 //Step 4 - Create Connection in your viewDidLoad Like 

     [self createConnection:@"http://www.google.com"];//give your valid url.

-(void)createConnection:(NSString *)urlString
{
     NSURL *url = [NSURL URLWithString:urlString];

  //Step 5 - parser delegate methods are using NSURLConnectionDelegate class or not.
    BOOL success;
    if (!parser)
    {
      parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
      parser.delegate = self;
      parser.shouldResolveExternalEntities = YES;
      success = [parser parse];
      NSLog(@"Success : %c",success);
    }
  }

   -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  {
      NSLog(@"Current Element Name : %@",elementName);

     if ([elementName isEqualToString:@"ID"])
     {
         NSLog(@"The Result is==%@",elementName);
     }

  }

  -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
  {
    currentStringValue = [[NSMutableString alloc] initWithString:string];
     NSLog(@"Current String Value : %@",currentStringValue);
  }

  -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
   {
     if ([elementName isEqualToString:@"ID"])
     {
       [arrayResult addObject:currentStringValue];
     }
      currentStringValue = nil;
  }