XMLParsing in IPhone for Login - iphone

I am following the below link to parse the xml for login page, http://yksoftware.blogspot.in/2010/04/iphone-programming-tutorial-xml-login.html
whenever i enter the fields of username and password, it displays login failed only even when i provide the correct username and password in the textfields.
In the following code when i keep the breakpoint in loginPressed method and check, it is not entering the for loop,
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
users = [[NSMutableArray alloc]init];
NSURL *xmlURL = [NSURL URLWithString:#"http://www.mailrail.net/sample.aspx?username=naresh&password=reddy"];
xmlParser = [[NSXMLParser alloc]initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"User"]) {
item = [[NSMutableDictionary alloc] init];
currentUser =[[NSMutableString alloc] init];
currentPassword =[[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:#"User"]) {
[item setObject:currentUser forKey:#"username"];
[item setObject:currentPassword forKey:#"password"];
[users addObject:[item copy]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([currentElement isEqualToString:#"username"]) {
[currentUser appendString:string];
}
if ([currentElement isEqualToString:#"password"]) {
[currentPassword appendString:string];
}
}
-(IBAction)loginPressed:(id)sender{
for (NSMutableDictionary *val in users) {
NSMutableString *usrname = [val objectForKey:#"username"];
NSLog(#"%#",usrname);
NSMutableString *psswrd = [val objectForKey:#"password"];
usrname=[usrname stringByReplacingOccurrencesOfString:#"\n" withString:#""];
usrname=[usrname stringByReplacingOccurrencesOfString:#"\t" withString:#""];
psswrd=[psswrd stringByReplacingOccurrencesOfString:#"\n" withString:#""];
psswrd=[psswrd stringByReplacingOccurrencesOfString:#"\t" withString:#""];
if([usrname isEqualToString:[txtUsername text]]&&[psswrd isEqualToString:[txtPassword text]]){
[lblLoginStatus setText:#"Login Successful!!"];
return;
}
}
[lblLoginStatus setText:#"login failed"];
return;
}
-(IBAction)returnClicked:(UITextField *)sender{
[sender resignFirstResponder];
}
-(IBAction)clickBackground:(id)sender{
[txtPassword resignFirstResponder];
[txtUsername resignFirstResponder];
}
and returns with login failed.
Thanks in advance.

You first need to solve the parsing how to parse some thing from xml using parser and then think for loging beacuse if you have made parser then it is easy to use login form. Below is the following tutorial for XML parsing it is detailed and help you can get help.
http://www.edumobile.org/iphone/iphone-programming-tutorials/parsing-an-xml-file/
hope this helps

Just check this
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = [elementName copy];
if ([elementName isEqualToString:#"User"]) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];
[item setValue:[attributeDict valueForKey:#"username"] forKey:#"username"];
[item setValue:[attributeDict valueForKey:#"password"] forKey:#"password"];
[users addObject:item];
}
}
please del other two delegates and check this

Related

How to parse the xml response string while parser Error 81(NSXMLParserInvalidEncodingError) in iPhone [duplicate]

<NewDataSet>
<Table>
<CaseId>743</CaseId>
<PartyId>11100550</PartyId>
<CartId>18</CartId>
</Table>
<Table>
<CaseId>742</CaseId>
<PartyId>11100549</PartyId>
<CartId>1148</CartId>
<BusinessID>19</BusinessID>
</Table>
</NewDataSet>
NSData* data = [xmlResponseData dataUsingEncoding:NSUTF8StringEncoding];
// NSXMLParser *XMLparser = [[NSXMLParser alloc] initWithData:data];
// [XMLparser setDelegate:self];
BOOL success;
if (XMLparser) // addressParser is an NSXMLParser instance variable
[XMLparser release];
XMLparser = [[NSXMLParser alloc] initWithData:data];
[XMLparser setDelegate:self];
[XMLparser setShouldResolveExternalEntities:YES];
success = [XMLparser parse]; // return value not used
// if not successful, delegate is informed of error
if(success)
NSLog(#"Sucess Parsed");
else
NSLog(#"Error Error Error!!!");
// NSLog(#"Parsed string : %#",valueForItem);
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"NewDataSet"]) {
caseID_List = [[NSMutableArray alloc]init];
}
NSLog(#"Processing Element: %#", elementName);
if ([elementName isEqualToString:#"CaseId"]) {
}
}
- (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:#"NewDataSet"])
return;
}
I need only CaseId to store from the given xml. Can any one advice me on this simple parsing!
For me its looping! and getting all details
Use the Below Code.Where Table is NSObject Class with the CaseId,PartyId,CartId as a properties in this class.If you have the xml url just called loadXMLByURL method with URl.After parsing you will get Each Object in TableArray which have the Table object with above properties.
NSMutableString *currentNodeContent;
NSXMLParser *parser;
Tweet *currentTweet;
bool isStatus;
-(id) loadXMLByURL:(NSString *)urlString
{
_tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:#"Table"])
{
currentTable = [Table alloc];
isStatus = YES;
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if (isStatus)
{
if ([elementname isEqualToString:#"CaseId"])
{
currentTable.CaseId = currentNodeContent;
}
if ([elementname isEqualToString:#"PartyId"])
{
currentTable.PartyId = currentNodeContent;
}
if ([elementname isEqualToString:#"CartId"])
{
currentTable.CartId = currentNodeContent;
}
}
if ([elementname isEqualToString:#"Table"])
{
[self.tableArray addObject:currentTable];
currentTable = nil;
currentNodeContent = nil;
}
}
Let me know if you have any doubt.

NSXMLParser how to pass the NSMutableDictionary to a NSMutableArray

I would like to pass the nsdictionary I am creating into an nsmutablearray but I'm not sure when or how to do it in the nsxmlparser delegates.
this is what I have done so far
#pragma mark - Parsing lifecycle
- (void)startTheParsingProcess:(NSData *)parserData
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //parserData passed to NSXMLParser delegate which starts the parsing process
[parser setDelegate:self];
[parser parse]; // starts the event-driven parsing operation.
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"item"]) {
valueDictionary = [[NSMutableDictionary alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSMutableString *dicString = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
currentElement = dicString;
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"title"]) {
titleString = currentElement;
[self.valueDictionary setObject:titleString forKey:#"title"];
NSLog(#"%#", [valueDictionary objectForKey:#"title"]);
NSLog(#" ");
currentElement = nil;
}
if ([elementName isEqualToString:#"description"])
{
descriptionString = currentElement;
[self.valueDictionary setObject:descriptionString forKey:#"description"];
NSLog(#"%#", [valueDictionary objectForKey:#"description"]);
NSLog(#" ");
currentElement = nil;
}
In -parser:didEndElement:namespaceURI:qualifiedName:, listen for the end of the item element, then add valueDictionary to a mutable array instance on your class.
if ([elementName isEqualToString:#"item"])
{
[self.mutableArrayOfDictionaries addObject:self.valueDictionary];
}
if ([elementName isEqualToString:#"title"]) {
titleString = currentElement;
[self.valueDictionary setObject:titleString forKey:#"title"];
NSLog(#"%#", [valueDictionary objectForKey:#"title"]);
NSLog(#" ");
currentElement = nil;
}
if ([elementName isEqualToString:#"description"])
{
descriptionString = currentElement;
[self.valueDictionary setObject:descriptionString forKey:#"description"];
NSLog(#"%#", [valueDictionary objectForKey:#"description"]);
NSLog(#" ");
currentElement = nil;
}

Leak occur at the time of Parsing

I'm actually doing parsing at the start of my program and this is my parser.m class i found
"leak" in this class in method "foundCharacters" in line
currentElementValue = [[NSMutableString alloc] initWithString:string];
currentElementValue is NSMutableString which is declare in parser.h file
NSMutableString *currentElementValue;
Can any one suggest me the way to solve this leak which some times crash my app in the starting...
Thanks In Advance...
didStartElement Method:-
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"posts"]) {
appDelegate.newsArray = [[NSMutableArray alloc] init];
}
else
{
if([elementName isEqualToString:#"page"])
{
aNewsInfo = [[NewsInfo alloc] init];
aNewsInfo.page = [[attributeDict objectForKey:#"id"] integerValue];
}
if(![elementName compare:#"smallimage"])
{
currentElementValue = [NSMutableString string];
}
if(![elementName compare:#"largeimage"])
{
currentElementValue = [NSMutableString string];
}
}
NSLog(#"Processing Element :%#",elementName);
}
Leak found in this foundCharacter method:- in line....
currentElementValue = [[NSMutableString alloc] initWithString:string];
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
didEndElement method:-
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"posts"])
return;
if([elementName isEqualToString:#"page"]) {
[appDelegate.newsArray addObject:aNewsInfo];
NSLog(#"%d",[appDelegate.newsArray count]);
NSLog(#"%#",aNewsInfo.title);
NSLog(#"%#",aNewsInfo.fulltext);
[aNewsInfo release];
aNewsInfo = nil;
}
else {
if ([elementName isEqualToString:#"smallimage"])
{
NSURL *url = [NSURL URLWithString:currentElementValue];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[aNewsInfo setSmallImageData:image];
}
if(![elementName compare:#"largeimage"])
{
NSURL *imageURL = [NSURL URLWithString:currentElementValue];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
[aNewsInfo setLargeImageData:image];
[image release];
}
[aNewsInfo setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
}
First of all you need to do some study:
Go to following links:
http://www.cocoadev.com/index.pl?MemoryManagement
http://mattpatenaude.com/ch3-memory-management.html
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/memorymgmt/Articles/MemoryMgmt.html
then do this,
currentElementValue = [[[NSMutableString alloc] initWithString:string] autorelease]
and remove [currentElementValue release], from
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
(otherwise it will leads to crash due to over releasing)

Parse 2 RSS feeds in app delegate

I'm following a tutorial and it's working fine but if I wanted to parse 2 RSS feeds it seems to overwrite one array instead of saving them in the respective arrays.
This is in my delegate:
NSURL *url2 = [[NSURL alloc] initWithString:#"myRSSFEED1"];
NSXMLParser *xmlParser1 = [[NSXMLParser alloc] initWithContentsOfURL:url2];
//Initialize the delegate.
XMLParser1 *parser1 = [[XMLParser1 alloc] initXMLParser];
//Set delegate
[xmlParser1 setDelegate:parser1];
//Start parsing the XML file.
BOOL successs = [xmlParser1 parse];
if(successs)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
//VIDS
NSURL *url = [[NSURL alloc] initWithString:#"MYRSSFEED2"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
that parses feed 1, allocates it to the array then parses 2 and seems to overwrite the first insteaf of using the second array that are defined as
#synthesize pics;
#synthesize books;
And saved in my XMLParser & XMLParser1
I can't figure out how to stop it from overwriting.
Here is my XMLParsers too:
- (void)parsers:(NSXMLParser *)parsers didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate2.pics = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
apics = [[BookPhoto alloc] init];
//Extract the attribute here.
apics.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading HAVid value :%i", apics.bookID);
}
NSLog(#"Processing Element: %#", elementName);
}
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
- (void)parsers:(NSXMLParser *)parsers didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate2.pics addObject:apics];
[apics release];
apics = nil;
}
else
[apics setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
And my XMLParser.m
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:#"Books"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:#"Book"]) {
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:#"id"] integerValue];
NSLog(#"Reading id value :%i", aBook.bookID);
}
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:#"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:#"Book"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
Any help on this would be brilliant,
I suggest putting a breakpoint at the point where the array is not getting loaded, and seeing if (1) it even gets called, and (2), whether it is saving the data into the array at all.
my bad
seems i had extra s's in parser:
- (void)parsers:(NSXMLParser *)parsers foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
should have been:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(#"Processing Value: %#", currentElementValue);
}
cheers for the help tho

Using NSXMLParser with CDATA

I'm parsing an RSS feed with NSXMLParser and it's working fine for the title and other strings but one of the elements is an image thats like
<!CDATA <a href="http:image..etc>
How do I add that as my cell image in my table view? Would I define that as an image type?
This is what i'm using to do my parsing:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
//NSLog(#"found this element: %#", elementName);
currentElement = [elementName 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];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//NSLog(#"ended element: %#", elementName);
if ([elementName isEqualToString:#"item"]) {
// save values to an item, then store that item into the array...
[item setObject:currentTitle forKey:#"title"];
[item setObject:currentLink forKey:#"link"];
[item setObject:currentSummary forKey:#"description"];
[item setObject:currentDate forKey:#"date"];
[stories addObject:[item copy]];
NSLog(#"adding story: %#", currentTitle);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//NSLog(#"found characters: %#", string);
// save the characters for the current item...
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];
}
}
You can use
- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{
NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}
The point of CDATA is that anything within it is not treated as part of the XML document.
So if you have tags in CDATA the parser will ignore them.
I'm guessing this CDATA is in the description element. So you'll need to extract the tags from the "description" element, either manually or via another instance of a parser.