Hi all
for so many days i tried to parse a simple xml file but with no luck at all.
here is how i do it...
first i send the user name and the password from my app using the post method then i use nsxmlparser to parse the xml created by the php file and get the user ID..
the user id and other user infos are stored in a table.
this is the post method ...
this is the php file ...
now you can see that the user id should be stored in the $nID variable but for some reason the parser can not read it.
this is the parser code (.m file) :
this is the log and you can see that the replystring is capturing the xml created by the .php file but the parser is failing to work with it ...
if i removed the If statement from the .php file then this is what i get ..
as you can see now, it reads the whole xml file but does not show the value of <ID> !!
one more thing... if i hardcoded the user name and password in the .php file then the parser will work just fine !!
i am really tired finding whats wrong and what mistake i am doing. i really appreciate any help and any suggestion.
thank you all in advance.
for so many days i tried to parse a simple xml file but with no luck at all.
here is how i do it...
first i send the user name and the password from my app using the post method then i use nsxmlparser to parse the xml created by the php file and get the user ID..
the user id and other user infos are stored in a table.
this is the post method ...
Code:
-(IBAction) login {
NSError *error = nil;
NSHTTPURLResponse *response;
// this user name and password are stored in a table db
NSString *userName = @"xxx";
NSString *passwordy = @"password";
// assemble the POST data
NSString *post = [NSString stringWithFormat:
@"strUserName=%@&strPassword=%@",
userName,passwordy];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.myserver.com/folder/logg.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
// send it
NSData *serverReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *replyString = [[NSString alloc] initWithBytes:[serverReply bytes] length:[serverReply length] encoding: NSASCIIStringEncoding];
if([replyString isEqualToString:@"Invalid."]){ // i have not set the php code to output "invalid" so this will not work for now ...
NSLog(@"%@",replyString);
}
else {
NSLog(@"%@",replyString); // what ever i echo out of the php file - this is just to capture the whole output ...
[self performSelectorInBackground:@selector(parseXML) withObject:nil]; // it'll invok the parser
}
}
this is the php file ...
Code:
<?php
header('Content-Type:text/xml;charset=UTF-8');
$strUserName = $_POST['strUserName'];
$strPassword = md5($_POST['strPassword']);
$connect = mysql_connect("localhost", "mxmxmxm", "bc5Fo0");
mysql_select_db("mydb");
$sqllogin = "SELECT usr_ID FROM tbluser WHERE usr_UserName='$strUserName' AND usr_Password='$strPassword'";
$nResult = mysql_query($sqllogin);
if( mysql_num_rows( $nResult ) == 1) {
$rstRow = mysql_fetch_array($nResult);
$nID = $rstRow['usr_ID'];
$xmlBody ='<?xml version="1.0" encoding="UTF-8"?>';
$xmlBody .='<XML>';
$xmlBody .='<Data>';
$xmlBody .='<ID>'.$nID.'</ID>';
$xmlBody .='</Data>';
$xmlBody .='</XML>';
echo $xmlBody;
exit;
}
?>
now you can see that the user id should be stored in the $nID variable but for some reason the parser can not read it.
this is the parser code (.m file) :
Code:
- (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@"started");
}
- (void)parseXMLFileAtURL:(NSString *)URL
{
//you must then convert the path to a proper NSURL or it won't work
NSURL *xmlURL = [NSURL URLWithString:URL];
// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[rssParser setDelegate:self];
//Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:YES];
[rssParser setShouldReportNamespacePrefixes:YES];
[rssParser setShouldResolveExternalEntities:YES];
[rssParser parse];
}
-(void) parseXML {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * path = @"http://www.myserver.com/folder/logg.php";
[self parseXMLFileAtURL :path];
[pool drain];
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSLog(@"Error: %@", [parseError localizedDescription]);
}
- (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:@"Data"]) {
// clear out our rest item caches...
currentUserID = [[NSMutableString alloc] init]; // init & alloc here
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
NSLog(@"ended element: %@", elementName);
if ([elementName isEqualToString:@"ID"]) {
// save values to an item, then store that item into the array...
NSLog(@"%@",currentUserID);
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
//if (!currentElement) return;
NSLog(@"found characters: %@", string);
// save the characters for the current item...
if ([currentElement isEqualToString:@"ID"]) {
[currentUserID appendString:string];
NSLog(@"this is the found method the id is %@",currentUserID);
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
NSLog(@"all done!");
}
- (void)dealloc {
[super dealloc];
[rssParser release];
[currentElement release];
[currentUserID release];
}
this is the log and you can see that the replystring is capturing the xml created by the .php file but the parser is failing to work with it ...
Code:
2010-10-17 18:04:44.068 usingPOST[597:207] <?xml version="1.0" encoding="UTF-8"?><XML><Data><ID>2</ID></Data></XML>
2010-10-17 18:04:44.648 usingPOST[597:5907] Error: Operation could not be completed. (NSXMLParserErrorDomain error 5.)
if i removed the If statement from the .php file then this is what i get ..
Code:
2010-10-17 18:10:17.387 usingPOST[624:207] <?xml version="1.0" encoding="UTF-8"?><XML><Data><ID>2</ID></Data></XML>
2010-10-17 18:10:17.934 usingPOST[624:5907] started
2010-10-17 18:10:17.936 usingPOST[624:5907] found this element: XML
2010-10-17 18:10:17.938 usingPOST[624:5907] found this element: Data
2010-10-17 18:10:17.940 usingPOST[624:5907] found this element: ID
2010-10-17 18:10:17.942 usingPOST[624:5907] ended element: ID
2010-10-17 18:10:17.943 usingPOST[624:5907] ended element: Data
2010-10-17 18:10:17.945 usingPOST[624:5907] ended element: XML
2010-10-17 18:10:17.946 usingPOST[624:5907] all done!
as you can see now, it reads the whole xml file but does not show the value of <ID> !!
one more thing... if i hardcoded the user name and password in the .php file then the parser will work just fine !!
Code:
$strUserName = "xxx";
$strPassword = md5(password);
i am really tired finding whats wrong and what mistake i am doing. i really appreciate any help and any suggestion.
thank you all in advance.