I am parsing an xml file , it works and show me the data parsed in the console( for example : processing value for Speller ...), but they aren't added to the msmutablearray users. Here is some code. Where is the problem ? help please :
- (MyData *) initXMLParser {
[super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"user"]) {
NSLog(#"user element found – create a new instance of User class...");
user = [[User alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
}
NSLog(#"Processing value for : %#", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"user"]) {
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
}
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
} else {
[user setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
-(BOOL)parseDocumentWithData:(NSData *)data {
//NSString * filePath = [[NSBundle mainBundle] pathForResource:#"Users" ofType:#"xml"];
//data = [NSData dataWithContentsOfFile:filePath];
if (data == nil)
return NO;
// this is the parsing machine
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
// this class will handle the events
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:NO];
// now parse the document
BOOL ok = [xmlparser parse];
if (ok == NO)
NSLog(#"error");
else
NSLog(#"OK");
[xmlparser release];
return ok;
}
- (void) dealloc {
[currentElementValue release];
[super dealloc];
}
This seems to be the problematic part of your code
- (MyData *) initXMLParser {
[super init]; // change to self = [super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
You are not initializing self with the value of [super init];
And add [users release]; line to your dealloc call unless you release it elsewhere
change your didEndElement method with this:
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"user"]) {
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
}
}
Related
i can't figure out why i can't see a proper text when parsing XML page. My code is working but, when i try to output in NSLog, i can see following:
{
description = "\U0429\U0435\U043d\U043e\U043a \U0432 \U0434\U0430\U0440. \U041f\U0430\U043f\U0430 - \U043e\U0432\U0447\U0430\U0440 \U0412\U0415\U041e. \U041c\U0430\U043c\U0430 - \U043c\U0435\U0442\U0438\U0441 \U043b\U0430\U0431\U0440\U0430.";
}
I already did parse XML from example populated with English letters, so why i can't see a Russian text here? Or maybe it is not about the text and i made some mistakes?
There is a code from my NSObject class i use to get data from:
#implementation requestManager
-(id)initWithDelegate:(id<requestDelegate>)delegateObject{
self = [super init];
if (self){
self.delegate = delegateObject;
self.contentData = [NSMutableData data];
self.listOfItems = [NSMutableArray array];
}
return self;
}
-(void)loadXmlData{
NSURL *urlString = [NSURL URLWithString:#"http://*************url address**********"];
NSURLRequest *req = [NSURLRequest requestWithURL:urlString];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
[connection start];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.contentData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSXMLParser *parserObj = [[NSXMLParser alloc]initWithData:self.contentData];
parserObj.delegate = self;
[parserObj parse];
}
#pragma mark - XML PARSER DELEGATE METHODS
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:#"description"]) isDescription = YES;
if ([elementName isEqualToString:#"text"]) isText = YES;
if ([elementName isEqualToString:#"name"]) isName = YES;
if ([elementName isEqualToString:#"images"]){
self.currentPosition = [NSMutableDictionary dictionary];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"description"]) isDescription = NO;
if ([elementName isEqualToString:#"text"]) isText = NO;
if ([elementName isEqualToString:#"name"]) isName = NO;
if ([elementName isEqualToString:#"images"]){
[self.listOfItems addObject:self.currentPosition];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(#"%#", self.listOfItems);
}
-(void)parser:(NSXMLParser*)parser foundCharacters:(NSString *)string{
if (isDescription) [self.currentPosition setValue:string forKey:#"description"];
if (isText) [self.currentPosition setValue:string forKey:#"text"];
if (isName) [self.currentPosition setValue:string forKey:#"name"];
}
I have a UILabel and UIButton in different pages, I want to read their title from xml file,
but I got NULL Value, In my XML parser class when I used NSLOG I have my answer but when I want to set them I have NULL, Would you please help me in this implementation?
Thanks in advance!
my XML Parser :
- (Presentation1NameXML *) initXMLParser {
appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"Presentation1Name"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
else if ([elementName isEqualToString:#"Presentation"]) {
presentation = [[Presentation alloc] init];
presentation.pLabel = [attributeDict objectForKey:#"label"];
NSLog(#"khengggg %#",presentation.pLabel);
// }else if ([elementName isEqualToString:#"Slides"]){
// slides = [[Slide alloc] init];
}
if([elementName isEqualToString:#"slide"]) {
slid = [[Slide alloc] init];
NSLog(#"Processing Element: %#", elementName);
slid.index = [[attributeDict objectForKey:#"index"] integerValue];
NSLog(#"Reading index value :%i", slid.index);
slid.sLabel = [attributeDict objectForKey:#"label"];
NSLog(#"Reading label value :%#", slid.sLabel);
slid.identifier = [attributeDict objectForKey:#"identifier"];
NSLog(#"Reading identifier value :%#", slid.identifier);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Presentation1Name"]){
return;
}
if([elementName isEqualToString:#"Presentation"]) {
[appDelegate.books addObject:presentation];
presentation = nil;
}
// if ([elementName isEqualToString:#"Slides"]){
// [appDelegate.books addObject:slides];
// slides = nil;
// return;
// }
if ([elementName isEqualToString:#"slide"]){
[appDelegate.books addObject:slid];
slid = nil;
}else
[presentation setValue:currentElementValue forKey:#"pLabel"];
//[presentation setValue:currentElementValue forKey:elementName];
currentElementValue = nil;
}
my label code : I got (null)
appDelegate = (testAppDelegate *)[[UIApplication sharedApplication] delegate];
label.textColor = [UIColor greenColor];
Presentation *p1 = [appDelegate.books objectAtIndex:0];
NSLog(#"aaaaaaaaa aaaa %#", p1);
label.text = p1.pLabel;
My Button code : I got (null)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
Slide *s1 = [appDelegate.books objectAtIndex:0];
NSLog(#" test %#", s1.sLabel);
My Object class Presentation link
My Object class Slide link
My XML file
The problem is here
if ([elementName isEqualToString:#"Presentation1Name"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
There is no such tag (Presentation1Name) and the array will never be created.
If you want to check NULL values, you can use this code
[str isEqual:[NSNull null]
This returns you if that value iS NULL from web services.
Hope this helps you!
modify your code in function didStartElement,there is no this tag 'Presentation1Name'
if ([elementName isEqualToString:#"Presentation"]) {
appDelegate.books = [[NSMutableArray alloc] init];
NSLog(#"The Prices Count");
}
and In function didEndElement you should delete this line :
[presentation setValue:currentElementValue forKey:#"pLabel"];
and also some memory leaks,you should release after addObject
if([elementName isEqualToString:#"Presentation"]) {
[appDelegate.books addObject:presentation];
[presentation release];
presentation = nil;
}
if ([elementName isEqualToString:#"slide"]){
[appDelegate.books addObject:slid];
[slid release];
slid = nil;
}
you should release
hope it helpful..
i m parsing an rss feed and loading in a webview...wat i want is ..to place a custom activity indicator in the exact place..where the parsing begins and the place where the parsing ends....below is the code.
#implementation MenuAndWineListViewController
NSDictionary *dict;
UIAlertView * errorAlert;
- (void)viewDidLoad
{
self.title=#"Menu & WineList";
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
activityIndicator1 = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]autorelease];
activityIndicator1.frame=CGRectMake(0.0,0.0, 40.0, 40.0);
activityIndicator1.center=self.view.center;
[self.view addSubview:activityIndicator1];
NSURL *baseURL=[[NSURL
URLWithString:#"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com /?feedpages&max=0&sort_order=ASC&parent=12&child_of=12"]retain];
NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
connection1=[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
if ([stories count] == 0) {
path = #"http://www.riverstonechophouse.com.php5-22.dfw1-2.websitetestlink.com/?feedpages&max=0&
sort_order=ASC&parent=180&child_of=180";
[self parseXMLFileAtURL:path];
}
[menuAndWineListViewController loadHTMLString:[dict objectForKey:#"description"] baseURL:nil];
[menuAndWineListViewController setClipsToBounds:YES];
menuAndWineListViewController.opaque=NO;
menuAndWineListViewController.backgroundColor=[UIColor clearColor];
[menuAndWineListViewController setDelegate:self];
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
NSLog(#"found file and started parsing");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
stories = [[NSMutableArray alloc] init];
NSURL *xmlURL = [NSURL URLWithString:URL];
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"item"])
{
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"item"])
{
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentSummary forKey:#"description"];
[stories addObject:[item copy]];
}
for (i=0 ; i<stories.count;i++)
{
dict = [stories objectAtIndex:i];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if ([currentElement isEqualToString:#"title"])
{
[currentTitle appendString:string];
}
else if ([currentElement isEqualToString:#"description"])
{
[currentSummary appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
[activityIndicator1 stopAnimating];
[activityIndicator1 removeFromSuperview];
NSLog(#"stories array has %d items", [stories count]);
}
I'd put the
[activityIndicator1 startAnimating];
at the beginning of the parseXMLFileAtURL method
and the
[activityIndicator1 stopanimating];
at the beginning of the parserDidEndDocument like you did.
I m reading the xml images from rss feed and parsing it in UITable view. Everything works fine, but it takes time to load the image content in the table view. The screen remains frozen. I'm using NSXMLParser to parse the image. Could you guys help me out, I'd be really greateful. Below is the code.
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName copy];
currentElement1=[attributeDict copy];
if ([elementName isEqualToString:#"item"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
//currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"url"])
{
currentString=[attributeDict objectForKey:#"url"];
// NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"url"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"item"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentContent forKey:#"content:encoded"];
[item setObject:currentDate forKey:#"pubDate"];
[stories addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"pubDate"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"content:encoded"]) {
[currentSummary appendString:string];
}
}
NSString *imagefile1 = [[stories objectAtIndex:indexPath.row]objectForKey:#"url"];
NSString *escapedURL=[imagefile1 stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
UIImage *image1 = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:escapedURL]]];
cell.imageView.image=image1;
[image1 release];
cell.textLabel.backgroundColor=[UIColor clearColor];
cell.textLabel.numberOfLines=2;
cell.textLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"title"];
cell.detailTextLabel.backgroundColor=[UIColor clearColor];
cell.detailTextLabel.numberOfLines=3;
cell.detailTextLabel.text=[[stories objectAtIndex:indexPath.row] objectForKey: #"pubDate"];
Use Lazy Loading to load images....
somewhat dated but should put you in the right direction
http://kosmaczewski.net/2009/03/08/asynchronous-loading-of-images-in-a-uitableview/
i m using nsxmlparser to read the apple itunes rss feed..could u guys help to read this particular xml image.
<im:image height="55">http://a1.phobos.apple.com/us/r1000/028/Music/5c/aa/fe/mzi.fsnbyjmf.55x55-70.jpg</im:image>
below is the code
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary*) attributeDict
{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"entry"]) {
// clear out our story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
currentString=[[NSMutableString alloc] init];
currentImage = [[NSMutableString alloc] init];
currentContent=[[NSMutableString alloc]init];
}
if ([attributeDict objectForKey:#"href"])
{
currentString=[attributeDict objectForKey:#"href"];
NSLog(#"what is my current string:%#",currentString);
[item setObject:currentString forKey:#"href"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI: (NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"entry"]) {
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"published"];
//[item setObject:currentImage forKey:#"im:image height=55"];
NSLog(#"the current image content:%#",item);
[stories addObject:[item copy]];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...///////////element
if ([currentElement isEqualToString:#"title"]) {
[currentTitle appendString:string];
} else if ([currentElement isEqualToString:#"link"]) {
[currentLink appendString:string];
} else if ([currentElement isEqualToString:#"description"]) {
[currentSummary appendString:string];
} else if ([currentElement isEqualToString:#"published"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:#"url"]) {
[currentContent appendString:string];
}
}
#user652878 try this coding its works well..... In didStartelement, write as follows.. else if([elementName isEqualToString:#"media:content"])
{
currentImage = [attributeDict valueForKey:#"url"];
}
In didEndElement, ....... else if([elementName isEqualToString:#"media:content"]){
[item setObject:currentImage forKey:#"image"];
}
In foundcharacters........................ else if([currentImage isEqualToString:#"media:content"])
{
[currentImage appendString:string];
}
use NSLog to see that we get image link or not.... Sure u will get ur solution...