如何使用字典为soap web服务ios存储数组值

时间:2016-02-26 11:45:39

标签: ios web-services soap xml-parsing

这里NsmutableString = textInProgress,count1是计数器管理。

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
 {
     NSLog(@"%d",count1);

     // Build the text value
     [textInProgress appendString:string];
     NSLog(@"%@",textInProgress);

     NSLog(@"%d",count1);
     count1=count1+1;
 }
  • 请有人指导我如何在数组中存储这个字符串值? 在这里,当我在日志中打印textInProgress时,它返回空值。

输出的某些部分: -

2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 0
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] (null)
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 0
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 2
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] (null)
2016-02-26 03:29:50.047 Hello_SOAP[3684:118829] 2

在任何其他方法中,我没有使用count1变量

的增量代替此方法

1 个答案:

答案 0 :(得分:5)

你已经创建了解析器的nsobject文件

parser.h

#import <Foundation/Foundation.h>
#import "BookDetail.h"
#import "AppDelegate.h"

@interface Parser : NSObject<NSXMLParserDelegate>
{
 AppDelegate *app;

NSMutableString *character;
BookDetail *book;
NSString *tempAuthorName;
NSMutableArray *booksArray;


 }
-(Parser *)initParser;
@property(strong,nonatomic)NSString *tempAuthorName;
@property (nonatomic,retain) NSMutableArray *booksArray;
@end

now parser.m

  #import "Parser.h"
  #import "ViewController.h"
  #import "DisplayInfoViewController.h"
  @implementation Parser
  @synthesize booksArray,tempAuthorName;


 -(Parser *)initParser
 {

     self = [super init];

  return self;
  }
  -(void) parser:(NSXMLParser *)parser didStartElement:(NSString   *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
  {
  if([elementName isEqualToString:@"books"])
   {
     booksArray = [[NSMutableArray alloc]init];
   }
    else if ([elementName isEqualToString:@"book"])
   {
    book = [[BookDetail alloc]init];
   }



    }
   -(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {

      string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
     if (!character)

    character=[[NSMutableString alloc] initWithString:string];

else
    [character appendString:string];
 }
      -(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  {

if([elementName isEqualToString:@"isbn"])
{
    book.isbn = character;

}
else if ([elementName isEqualToString:@"title"])
{
    book.title = character;
}
else if ([elementName isEqualToString:@"author"])
{
    book.author = character;
}

else if ([elementName isEqualToString:@"publisher"])
{
    book.publisher = character;
}

else if ([elementName isEqualToString:@"amazon_price"])
{
    book.amazon_price = character;
}
else if ([elementName isEqualToString:@"book"])
{

    [booksArray addObject:book];


}

character = nil;



     }
     @end

xml文件

   <?xml version="1.0"?>
   <books>
    <book>
      <isbn>1594489501</isbn>
      <title>A Thousand Splendid Suns</title>
      <author>Romio</author>
      <publisher>Riverhead Hardcover</publisher>
     <amazon_price>14.27</amazon_price>
  </book>
  <book>
    <isbn>1594489587</isbn>
    <title>The Brief Wondrous Life of Oscar Wao</title>
    <author>Junot Diaz</author>
    <publisher>Riverhead Hardcover</publisher>
    <amazon_price>14.97</amazon_price>
 </book>
 <book>
    <isbn>0545010221</isbn>
   <title>Harry Potter and the Deathly Hallows</title>
   <author>J. K. Rowling</author>
   <publisher>Arthur A. Levine Books</publisher>
   <amazon_price>19.24</amazon_price>
 </book>
</books>

BookDetail是另一个帮助处理数据的nsobject类          BookDetail.h

    #import <Foundation/Foundation.h>

   @interface BookDetail : NSObject
   @property(nonatomic,retain)NSString *isbn;
   @property(nonatomic,retain)NSString *title;
   @property(nonatomic,retain)NSString *author;
   @property(nonatomic,retain)NSString *publisher;
   @property(nonatomic,retain)NSString *amazon_price;


   @end

的.m

   #import "BookDetail.h"

   @implementation BookDetail
   @synthesize isbn,title,author,publisher,amazon_price;

   @end

要获取表格单元格上的数据,可能会对您有帮助..

     - (UITableViewCell *)tableView:(UITableView *)tableView1   cellForRowAtIndexPath:(NSIndexPath *)indexPath
      {


          static NSString *identifier = @"simpleIdentifier";
           TableViewCell *cell = [tableView1  dequeueReusableCellWithIdentifier:identifier];
          if(cell == nil)
          {
              NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
              cell = [nib objectAtIndex:0];
           }
          BookDetail *ss=[parse.booksArray objectAtIndex:indexPath.row];
         cell.bookName.text=ss.author;
         return cell;
      }

     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
          DisplayInfoViewController *displayinfoViewController=[[DisplayInfoViewController alloc] initWithNibName:@"DisplayInfoViewController" bundle:nil];
           BookDetail *ss=[parse.booksArray objectAtIndex:indexPath.row];
         displayinfoViewController.isbnValue=ss.isbn;
         displayinfoViewController.titleValue=ss.title;
         displayinfoViewController.amazonValue=ss.amazon_price;
         displayinfoViewController.authorValue=ss.author;
         displayinfoViewController.publisherValue=ss.publisher;


         [self.navigationController pushViewController:displayinfoViewController animated:YES];



         }