[h파일]
typedef enum {
etNone = 0,
etItem
} eElementType;
@interface 이름ViewController : UIViewController {
NSMutableData *receiveData;
eElementType elementType;
NSMutableArray *xmlParseData;
NSMutableString *xmlValue;
}
[m파일]
-(IBAction)buttonPressed:(id)sender
{
NSDate *now = [NSDate date];
NSString *bodyString = [[NSString alloc] initWithFormat:@"date=%@",now];
NSString *url=@"http://address/test.php"; //xml address
// URL 접속 초기화
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:15.0];
[request setHTTPMethod:@"POST"]; // POST로 선언하고
[request setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
xmlParseData = [[NSMutableArray alloc] init];
xmlValue = [[NSMutableString alloc] init];
receiveData = [[NSMutableData data] retain]; // 수신할 데이터를 받을 공간을 마련
NSLog(@"success con");
}
NSString *stitle = [[NSString alloc] initWithFormat:@"%@ 등록확인",sname.text];
NSString *message = [[NSString alloc] initWithFormat:@"등록되었습니다."];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:stitle message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
[stitle release];
[message release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[receiveData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *data;
data = [[NSString alloc] initWithData:receiveData encoding:NSUTF8StringEncoding];
NSLog(data); NSXMLParser *parser = [[NSXMLParser alloc] initWithData:receiveData];
[parser setDelegate:self];
[parser parse];
[parser release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[receiveData release];
}
#pragma mark XMLParse delegate methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"Item"]) // 최상위 노드이름;
elementType = etItem;
// NSLog(@"parsing part start");
[xmlValue setString:@""];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSString *msg=@"";
//NSLog(@"parsing part");
if (elementType != etItem)
return;
if ([elementName isEqualToString:@"Title"]) {
msg = [ [ NSString alloc ] initWithFormat:@"i= %@" , [NSString stringWithString:xmlValue]];
NSLog(msg);
} else if ([elementName isEqualToString:@"Image"]) {
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (elementType == etItem) {
[xmlValue appendString:string];
}
}
댓글 달기