如何在iPhone中存储xml值并在数组中计数以及如何在iphone的tableview单元中显示值

如何解决如何在iPhone中存储xml值并在数组中计数以及如何在iphone的tableview单元中显示值

|| 嗨,朋友,谢谢我从xml中获取数据的结果,我可以在控制台中看到在控制台中看到的所有元素值,但是我的NSLog中的问题是问题(@ \“ mDataArray count =%d \”,[mParserArray count]); 我在mdataarray中看不到任何计数值 这是我的解析器代码,请脸颊一些,告诉我解析器代码在做什么错 请同时检查我正在创建并帮助我的朋友的类解析器和控制器类 这是我的parser.h文件----------------- /
#import \"TWeatherElement.h\"//this is the class where the elements are Created
#import <Foundation/Foundation.h>


@interface TWeatherParser : NSObject<NSXMLParserDelegate> 
{
    NSMutableArray *mParserArray;
    NSXMLParser *mXmlParser;
    NSMutableString *mCurrentElement;
    BOOL elementFound;
    TWeatherElement *mWeather;

}
@property (nonatomic,retain) NSMutableString *currentElement;
@property (nonatomic,retain)NSMutableArray *mParserArray;
@property (nonatomic,retain) TWeatherElement *weatherobj;

-(void)getInitialiseWithData:(NSData *)inData;

@end



this is mu parser.mfile---------------------------/


#import \"TWeatherParser.h\"
#import \"JourneyAppDelegate.h\"
#import \"api.h\"
#import \"TWeatherController.h\"
//#define kParsingFinishedNotification @\"ParsingFinishedNotification\"


@implementation TWeatherParser
@synthesize weatherobj = mWeather;
@synthesize currentElement = mCurrentElement;
@synthesize mParserArray;

//-(id)init
//{
//  if ([super init]) 
//  {
//      currentElement release=[[NSMutableString alloc]init];
//      mWeather =nil;
//
//  }
//  return self;
//}

-(void)getInitialiseWithData:(NSData *)inData
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:inData];
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:YES];   //YES if the receiver should report the namespace and qualified name of each element,NO otherwise. The default value is NO
    [parser setShouldReportNamespacePrefixes:YES]; //YES if the receiver should report the scope of namespace declarations,NO otherwise. The default value is NO.
    [parser setShouldResolveExternalEntities:NO];//YES if the receiver should report declarations of external entities,NO otherwise. The default value is NO

    [parser parse];
    NSLog(@\"%@\",parser);

    [parser release];
}



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

    if ([elementName isEqualToString:@\"xml_api_reply\"])
    {
        mWeather = [[TWeatherElement alloc]init];
        NSString *data8= [attributeDict objectForKey:@\"version\"];
        if(data8 !=nil)
            mWeather.xmlapireply =data8 ;
        [mParserArray addObject:data8];
    }
    if ([elementName isEqualToString:@\"weather\"])
    {
        NSString *data0= [attributeDict objectForKey:@\"module_id\"];
        if(data0 !=nil)
            mWeather.weather =data0 ;
        NSLog(@\"weather==%@\",[attributeDict valueForKey:@\"module_id\"]);
    }
     if([elementName isEqualToString:@\"current_date_time\"])
     {
         NSString *data1= [attributeDict objectForKey:@\"data\"];
         if (data1 !=nil) 
             mWeather.currentdate =data1;
         NSLog(@\"current_date_time==%@\",[attributeDict valueForKey:@\"data\"]);
     }
            if([elementName isEqualToString:@\"condition\"])
            {
                NSString *data2= [attributeDict objectForKey:@\"data\"];
                if (data2 !=nil) 
                    mWeather.conditionname=data2;
                NSLog(@\"condition==%@\",[attributeDict valueForKey:@\"data\"]);

            }
            if([elementName isEqualToString:@\"humidity\"])
            {
                NSString *data3= [attributeDict objectForKey:@\"data\"];
                if (data3 !=nil) 
                    mWeather.humidity =data3;
                NSLog(@\"humidity==%@\",[attributeDict valueForKey:@\"data\"]);

            }
            if([elementName isEqualToString:@\"icon \"])
            {
                NSString *data4= [attributeDict objectForKey:@\"data\"];
                if (data4 !=nil) 
                    mWeather.icon =data4;
                NSLog(@\"icon==%@\",[attributeDict valueForKey:@\"data\"]);

            }
            if([elementName isEqualToString:@\"wind_condition \"])
            {
                NSString *data5= [attributeDict objectForKey:@\"data\"];
                if (data5 !=nil) 
                    mWeather.wind =data5;
                NSLog(@\"wind_condition==%@\",[attributeDict valueForKey:@\"data\"]);
            }
            if([elementName isEqualToString:@\"low \"])
            {
                NSString *data6= [attributeDict objectForKey:@\"data\"];
                if (data6 !=nil) 
                    mWeather.mintemp = data6;
                NSLog(@\"low==%@\",[attributeDict valueForKey:@\"data\"]);
            }
            if([elementName isEqualToString:@\"high \"])
            {
                NSString *data7= [attributeDict objectForKey:@\"data\"];
                if (data7 !=nil) 
                    mWeather.maxtemp =data7;
                NSLog(@\"high==%@\",[attributeDict valueForKey:@\"data\"]);
            }
    //{
//      self.currentElement = [NSMutableString string];
//  }
//  else 
//  {
//      self.currentElement = nil;
//  }


}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString*)string
{
    if (nil!= self.currentElement)
    {
        [self.currentElement appendString:string];
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qName
{
    if (nil != qName)
    {
        elementName  = qName;
    }
    if ([elementName isEqualToString:@\"current_date_time \"]) 
    {
        mWeather.currentdate = self.currentElement;

    }
else if ([elementName isEqualToString:@\"condition \"]) 
{
    mWeather.conditionname = self.currentElement;

}
else if ([elementName isEqualToString:@\"humidity \"]) 
{
    mWeather.humidity = self.currentElement;

}
else if ([elementName isEqualToString:@\"icon \"]) 
{
    mWeather.icon = self.currentElement;

}
else if ([elementName isEqualToString:@\"wind_condition \"]) 
{
    mWeather.wind = self.currentElement;

}
else if ([elementName isEqualToString:@\"low \"]) 
{
    mWeather.mintemp = self.currentElement;

}
else if ([elementName isEqualToString:@\"high \"]) 
{
    mWeather.maxtemp = self.currentElement;

}
else if ([elementName isEqualToString:@\"weather\"]) 
{
    [mParserArray addObject:mWeather];
    NSLog(@\"mDataArray count = %d\",[mParserArray count]);
    [mWeather release];

}   
}

//-(void)parserDidEndDocument:(NSXMLParser *)parser
//{
//  [[NSNotificationCenter defaultCenter ]postNotificationName:kParsingFinishedNotification object:mParserArray];
//}

-(void)dealloc
{
    [super dealloc];
    self.weatherobj = nil;
    self.currentElement = nil;
}
@end

and this is my controller class where i want to show all parser value in table view cell in iphone but it not working proper i think my code is not proper in controller class how i can show all data in controller class please help some one
this is TWeatherController.h-------------------------/

#import <UIKit/UIKit.h>
#import \"TWeatherParser.h\"
@class TWeatherParser;


@interface TWeatherController : UITableViewController {

    UITableView *mTableView;
    NSMutableArray *mImage;
    NSMutableArray *weatherarray;
    TWeatherParser *weather;




}
@property (nonatomic,retain) IBOutlet UITableView *mTableView;


@end


this is TWeatherController .m----------------------/

#import \"TWeatherController.h\"
#import \"TWeatherCell.h\"
#import \"TWeatherElement.h\"
#import \"TWeatherParser.h\"
#import \"api.h\"


@implementation TWeatherController
@synthesize mTableView;


#pragma mark -
#pragma mark Initialization

- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    style = UITableViewStyleGrouped;
    if (self = [super initWithStyle:style]) {
    }
    return self;
}



#pragma mark -
#pragma mark View lifecycle

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    api *ap = [[api alloc]init];
    NSData *aData = [ap getBusXMLAtStop:@\"1\"];
    NSString *str = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];

    //NSInteger value = [str intValue];
    if (str)
    {
        NSLog(@\"this is success %@\",ap.dataReply);
        TWeatherParser *parser = [[TWeatherParser alloc]init];
        [parser getInitialiseWithData:ap.dataReply];
        [parser release];


    }
    else 
    {
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@\"Alert\" message:@\"cannot fetch\" delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
        [alertview show];
        [alertview release];
    }


    [ap release];



}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    //TWeatherParser *parse = [[TWeatherParser alloc]init];
    //weatherarray = parse.mParserArray;
    return [weatherarray count];

}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @\"Cell\";


   TWeatherCell *cell =(TWeatherCell *) [mTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TWeatherCell alloc] initWithStyle:UITableViewStyleGrouped reuseIdentifier:CellIdentifier] autorelease];
    }
    TWeatherElement *newobj = [weather.mParserArray objectAtIndex:indexPath.row];
    if ([newobj.icon isEqualToString:@\"http://\\n\"])
    {
        cell.weatherimage.image = [UIImage imageNamed:@\"listIcon-H.png\"];
    }
    else {
        NSData *imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:newobj.icon]];
        cell.weatherimage.image = [UIImage imageWithData:imageData];
        [imageData release];
    }
    cell.reportdate.text = newobj.currentdate;
    cell.conditionname.text = newobj.conditionname;
    cell.twotemp.text = [NSString stringWithFormat:@\"Temp:%@/%@\",newobj.mintemp,newobj.maxtemp];
    cell.twodirection.text = newobj.wind;
    cell.humidity.text = newobj.humidity;
    //cell.reportdate.text = newobj.currentdate;


    //cell.reportdate.text =@\"My journey\";
//  cell.conditionname.text = @\"raji\";
//  cell.twotemp.text = @\"pradeep\"; 
//  cell.twodirection.text = @\"harish\";
//  cell.humidity.text =@\"23\";
//  cell.weatherimage.image = [UIImage imageNamed:@\"listIcon-H.png\"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    // Configure the cell...

    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class,insert it into the array,and add a new row to the table view.
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@\"<#Nib name#>\" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
{
    return 100.0;


}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn\'t have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data,images,etc. that aren\'t in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
    

解决方法

        
NSLog(@\"this is success %@\",ap.dataReply);
        TWeatherParser *parser = [[TWeatherParser alloc]init];
        [parser getInitialiseWithData:ap.dataReply];
        [parser release];
初始化your2ѭ 在这里,您需要将已解析的数组存储到某个变量中。你不是在做。在您的代码“ 3”中,变量应包含解析后的值。 我希望这可以为您指明正确的方向。使
self.weatherarray
指向
parser. mParserArray
或使用您的计划。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-