XML's Parsing problem
I had a problem in xml's parsing.
I got two xml files....
example xml file first:
<Root1>
<example1 att1="data" att2="data2" att3="data"/>
</Root1>
xml file second.
<Root2>
<example2 att1="data"/>
</Root2>
I need to parse this two xml in my project.
for first xml file I created Class for FirstClass which holds all the attributes in that class. example
class FirstClass
{
NSString att1;
NSString att2;
NSString att3;
}
give property and synth.
I created FirstXMLParser for first xml file.
-开发者_Python百科(FirstXMLParser *)initXMLParser
{
[super init];
appDelegate=(PartyTemperature_AppDelegate *)[[UIApplication sharedApplication]delegate];
return self;
}
didstartelement and didendelement. i worked on that... I parsed XML perfect....
In my FirstviewController i parsed xml successfully .
for second I created XMLParserSecond I created classSecond
ClassSecond
{
NSString att1;
}
set property and synth.
Created SecondXMLParser
-(SecondXMLParser *)initDJXMLParser{
[super init];
appDelegate=(PartyTemperature_AppDelegate *)[[UIApplication sharedApplication]delegate];
return self;
}
while parsing in my SecondViewController my app get crashed.
I dont know what to do.....
I hope you people can understand my problem
Please Help me out.
if you want i can post my code......
@Thanks in Advance.
The best approach is that heave XML parser for first XML data in FirstXMLParser (implementing XML parsing may be NSXMLParser) then second XML data in SecondXMLParser (implementing XML parsing may be NSXMLParser). Note: If you are using NSXMLParser then heave required delegate in the both class.
The each class should take the XML data as input, then process it then and store the parsed data in the member variable.
Which ever module of your application needs to parse the XML content uses the object of respective class object and get the output.
If you need more details please share me you code and flow --- I hope this explanation helps you.
Source Code. xml parser first and second.
#import <Foundation/Foundation.h>
@class EventInfo;
@class DistanceInfo;
@class PartyTemperature_AppDelegate;
@protocol NSXMLParserDelegate;
@interface XMLParser : NSObject <NSXMLParserDelegate>{
EventInfo *aEventInfo;
DistanceInfo *aDistanceInfo;
PartyTemperature_AppDelegate *appDelegate;
}
-(XMLParser *)initXMLParser;
@end
//Implementation File
#import "XMLParser.h"
#import "EventInfo.h"
#import "PartyTemperature_AppDelegate.h"
#import "DistanceInfo.h"
@implementation XMLParser
-(XMLParser *)initXMLParser
{
[super init];
appDelegate=(PartyTemperature_AppDelegate *)[[UIApplication sharedApplication]delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"root"]) {
appDelegate.distanceListArray=[[NSMutableArray alloc]init];
}
if ([elementName isEqualToString:@"events"]) {
aDistanceInfo=[[DistanceInfo alloc] init];
aDistanceInfo.eventListArray=[[NSMutableArray alloc]init];
aDistanceInfo.event_Distance=[attributeDict objectForKey:@"distance"];
}
if ([elementName isEqualToString:@"event"]) {
aEventInfo=[[EventInfo alloc]init];
aEventInfo.event_ID=[attributeDict objectForKey:@"id"];
aEventInfo.event_Title=[attributeDict objectForKey:@"title"];
aEventInfo.event_Description=[attributeDict objectForKey:@"description"];
aEventInfo.event_Date=[attributeDict objectForKey:@"date"];
aEventInfo.event_Time=[attributeDict objectForKey:@"time"];
aEventInfo.event_Location=[attributeDict objectForKey:@"location"];
aEventInfo.event_Street=[attributeDict objectForKey:@"street"];
aEventInfo.event_City=[attributeDict objectForKey:@"city"];
aEventInfo.event_Visitors=[attributeDict objectForKey:@"visitors"];
aEventInfo.event_Organisation=[attributeDict objectForKey:@"organisation"];
aEventInfo.event_Price=[attributeDict objectForKey:@"price"];
aEventInfo.event_Minimum_Age=[attributeDict objectForKey:@"minimum_age"];
aEventInfo.event_Picture=[attributeDict objectForKey:@"picture"];
aEventInfo.event_Genre=[attributeDict objectForKey:@"genre"];
aEventInfo.event_LineUP=[attributeDict objectForKey:@"lineup"];
aEventInfo.event_WebSite=[attributeDict objectForKey:@"website"];
aEventInfo.event_latitude=[attributeDict objectForKey:@"latitude"];
aEventInfo.event_longitude=[attributeDict objectForKey:@"longitude"];
aEventInfo.event_distance=[attributeDict objectForKey:@"distance"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"root"]) {
return;
}
if ([elementName isEqualToString:@"events"]) {
[appDelegate.distanceListArray addObject:aDistanceInfo];
NSLog(@"appDelegate.distanceListArray %d",[appDelegate.distanceListArray count]);
}
if ([elementName isEqualToString:@"event"]) {
[aDistanceInfo.eventListArray addObject:aEventInfo];
}
NSLog(@"aDistanceInfo.eventListArray information %d",[aDistanceInfo.eventListArray count]);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
return;
}
SecondXMLParser
#import <Foundation/Foundation.h>
@protocol NSXMLParserDelegate;
@class DJInfo;
@class NearbyViewController;
@interface DJXMLParser : NSObject <NSXMLParserDelegate>{
DJInfo *aDJInfo;
NearbyViewController *aNearbyViewController;
}
-(DJXMLParser *)initDJXMLParser;
@end
#import "DJXMLParser.h"
#import "DJInfo.h"
#import "PartyTemperature_AppDelegate.h"
#import "NearbyViewController.h"
@implementation DJXMLParser
-(DJXMLParser *)initDJXMLParser{
[super init];
aNearbyViewController=(NearbyViewController *)[[UIApplication sharedApplication]delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict{
if ([elementName isEqualToString:@"root"]) {
aNearbyViewController.djListArray=[[NSMutableArray alloc] init];
}
if ([elementName isEqualToString:@"dj"]) {
aDJInfo=[[DJInfo alloc] init];
aDJInfo.dj_Name=[attributeDict objectForKey:@"name"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"root"]) {
return;
}
if ([elementName isEqualToString:@"dj"]) {
[aNearbyViewController.djListArray addObject:aDJInfo];
}
NSLog(@"aDJInfo.Value %@",[aNearbyViewController.djListArray count]);
}
@end
精彩评论