How do I parse this type of XML using NSXMLParser
<category> <categoryName> userName</categoryName> <categoryName>password</categoryName><category>
How do I parse this type of XML using NSXMLParser
<category> <categoryName> userName</categoryName> <categoryName>password</categoryName><category>
Declare array and a string in h file like:
NSMutableArray *aryCategory;
NSString *strCategroyName;
in .m file for starting Parser use:
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:yourData]; // your data will be instance of NSData or NSMutableData
parser.delegate = self;
[parser parse];
this will be done once you get your xml data. For handling result you can use NSXMLParserDelegate
as follows:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:@"category"]) {
aryCategory = [[NSMutableArray alloc] init];
strCategroyName = @"";
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
strCategroyName = [NSString stringWithFormat:@"%@%@", strCategroyName, string];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:@"categoryName"]) {
[aryCategory addObject:strCategroyName];
strCategroyName = @"";
}
}
Now you will have your array fill with all of your category name.
Hope this helps :)
Write the <category>
in didStart
and didEnd
elements which are the parser's delegates.