xml parsing run but no data displayed - objective-c

This is the code of my app . The applications runs but there is no data displayed , can you help me to fix this problem ?
In .h file:
#interface MyData : NSObject <NSXMLParserDelegate> {
NSMutableString *currentElementValue;
User *user;
NSMutableArray *users;
}
-(MyData *) initXMLParser;
-(BOOL)parseDocumentWithData:(NSData *)data;
#property (nonatomic, retain) User *user;
#property (nonatomic, retain) NSMutableArray *users;
In .m file:
#implementation MyData
#synthesize user;
#synthesize users;
- (MyData *) initXMLParser {
[super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
-(BOOL)parseDocumentWithData:(NSData *)data {
NSString * filePath = [[NSBundle mainBundle] pathForResource:#"Users" ofType:#"xml"];
data = [NSData dataWithContentsOfFile:filePath];
if (data == nil)
return NO;
// this is the parsing machine
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
// this class will handle the events
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:NO];
// now parse the document
BOOL ok = [xmlparser parse];
if (ok == NO)
NSLog(#"error");
else
NSLog(#"OK");
[xmlparser release];
return ok;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if ([elementName isEqualToString:#"user"]) {
NSLog(#"user element found – create a new instance of User class...");
user = [[User alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
}
NSLog(#"Processing value for : %#", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"user"]) {
// object to our user array
[users addObject:user];
// release user object
[user release];
user = nil;
} else {
[user setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
#end
/* This is the code of my app . The applications runs but there is no data displayed , can you help me to fix this problem ? */
This is the code of my app . The application runs but there is no data displayed , can you help me to fix this problem ? I want to display data when parsing

Well if you are using the NSXML parser (objective-c) you will need to append the data from the parsing of the xml element, to a string. How are you planning to display the data from the xml file.
You can apply the same method from here:
Techno Buffalo RSS
And then change the values in the parser to your element values.

Related

Parsing works but data are not stored in nsMutableArray

I am parsing an xml file , it works and show me the data parsed in the console( for example : processing value for Speller ...), but they aren't added to the msmutablearray users. Here is some code. Where is the problem ? help please :
- (MyData *) initXMLParser {
[super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:#"user"]) {
NSLog(#"user element found – create a new instance of User class...");
user = [[User alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (!currentElementValue) {
// init the ad hoc string with the value
currentElementValue = [[NSMutableString alloc] initWithString:string];
} else {
// append value to the ad hoc string
[currentElementValue appendString:string];
}
NSLog(#"Processing value for : %#", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"user"]) {
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
}
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
} else {
[user setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
-(BOOL)parseDocumentWithData:(NSData *)data {
//NSString * filePath = [[NSBundle mainBundle] pathForResource:#"Users" ofType:#"xml"];
//data = [NSData dataWithContentsOfFile:filePath];
if (data == nil)
return NO;
// this is the parsing machine
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
// this class will handle the events
[xmlparser setDelegate:self];
[xmlparser setShouldResolveExternalEntities:NO];
// now parse the document
BOOL ok = [xmlparser parse];
if (ok == NO)
NSLog(#"error");
else
NSLog(#"OK");
[xmlparser release];
return ok;
}
- (void) dealloc {
[currentElementValue release];
[super dealloc];
}
This seems to be the problematic part of your code
- (MyData *) initXMLParser {
[super init]; // change to self = [super init];
// init array of user objects
users = [[NSMutableArray alloc] init];
return self;
}
You are not initializing self with the value of [super init];
And add [users release]; line to your dealloc call unless you release it elsewhere
change your didEndElement method with this:
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName {
if ([elementName isEqualToString:#"users"]) {
// We reached the end of the XML document
return;
}
if ([elementName isEqualToString:#"userName"]) {
[[self user] setUserName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"firstName"]) {
[[self user] setFirstName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"lastName"]) {
[[self user] setLastName:currentElementValue];
[currentElementValue release];
currentElementValue = nil;
}
if ([elementName isEqualToString:#"user"]) {
[users addObject:user];
/*comboarray = [[users arrayForKey:#"ComboBoxValues"]
sortedArrayUsingSelector:#selector(compare:)];*/
// release user object
[user release];
user = nil;
}
}

Setting NSString variables with NSXMLParser

I am using NSXMLParser to grab information from an online XML file. My goal is to have one class do the XML parsing and another class to implement the variables. Below is the code for my project:
Current.h & Current.m
#import <Foundation/Foundation.h>
#interface Current : NSObject {
NSString *curTempF;
IBOutlet NSTextField *textField;
}
#property (nonatomic, copy) NSString *curTempF;
- (void)displayOutlets:(id)sender;
#end
and
#import "Current.h"
#implementation Current
#synthesize curTempF;
- (void)awakeFromNib {
[self displayOutlets:self];
}
- (void)displayOutlets:(id)sender {
[textField setStringValue:curTempF];
}
#end
XmlParser.h & XmlParser.m
#import <Foundation/Foundation.h>
#interface XmlParser : NSObject <NSXMLParserDelegate> {
NSString *urlString;
NSURL *url;
NSMutableString *xmlString;
}
- (IBAction)fetchXML:(id)sender;
#end
and
#import "XmlParser.h"
#import "Current.h"
#implementation XmlParser
- (void)awakeFromNib {
[self fetchXML:self];
}
- (IBAction)fetchXML:(id)sender {
urlString = #"http://api.wunderground.com/api/***/conditions/q/28173.xml";
url = [NSURL URLWithString:urlString];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqual:#"temp_f"]) {
xmlString = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if ([elementName isEqual:#"temp_f"]) {
Current *cTempF = [[Current alloc] init];
[cTempF setCurTempF:xmlString];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
[xmlString appendString:string];
}
#end
When I run the program I am receiving an error about "Invalid parameter not satisfying: aString". It looks like the setStringValue for the IBOutlet is not working. Any suggestions?
I suspect that when you initialize the Current class with [[Current alloc] init], it is trying to populate the textField with the curTempF string. However, the curTempF string is not initialized yet at that point until you call [cTempF setCurTempF:xmlString].
One approach would be to not display outlets in the Current class:
- (void)awakeFromNib {
//[self displayOutlets:self];
}
Then, call the displayOutlets function in your parser:
if ([elementName isEqual:#"temp_f"]) {
Current *cTempF = [[Current alloc] init];
[cTempF setCurTempF:xmlString];
[cTempF displayOutlets:cTempF];
}
Alternately, you could keep your awakeFromNib code the same, but create an initWith method in your Current class, which may be cleaner:
- (id) initWithCurTemp:(NSString *)curTemp {
self = [super init];
if (self) {
self.curTempF = curTemp;
}
return self;
}
- (void)awakeFromNib {
[self displayOutlets:self];
}
Then, your parser code would look like:
if ([elementName isEqual:#"temp_f"]) {
Current *cTempF = [[Current alloc] initWithCurTemp:xmlString];
}
You should create your Current in the nib, as you were doing, and not in code.
As for getting the parser (which I assume is in the same nib) to talk to the Current, create an outlet in the parser to point to the Current, and connect that outlet in the nib.

How to save core data with an xml parser

I made this xml parser :
XMLToObjectParser.m :
#import "XMLToObjectParser.h"
#implementation XMLToObjectParser
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (NSArray *)items
{
return items;
}
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error
{
//[items release];
items = [[NSMutableArray alloc] init];
className = aClassName;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];
if([parser parserError] && error) {
*error = [parser parserError];
}
//[parser release];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict
{
if([elementName isEqualToString:className]) {
// create an instance of a class on run-time
item = [NSEntityDescription entityForName:className inManagedObjectContext:managedObjectContext];
}
else {
currentNodeName = [elementName copy];
currentNodeContent = [[NSMutableString alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
//NSLog(#"Close tag: %#", elementName);
if([elementName isEqualToString:className]) {
[items addObject:item];
//[item release];
item = nil;
}
else if([elementName isEqualToString:currentNodeName]) {
// use key-value coding
[item setValue:currentNodeContent forKey:elementName];
//[currentNodeContent release];
currentNodeContent = nil;
//[currentNodeName release];
currentNodeName = nil;
}
}
- (void)parser:(NSXMLParser *)parser
foundCharacters:(NSString *)string
{
[currentNodeContent appendString:string];
}
- (void)dealloc
{
//[items release];
//[super dealloc];
}
#end
XMLToObjectParser.h :
#import <Foundation/Foundation.h>
#interface XMLToObjectParser : NSObject <NSXMLParserDelegate> {
NSString *className;
NSMutableArray *items;
NSObject *item; // stands for any class
NSString *currentNodeName;
NSMutableString *currentNodeContent;
NSManagedObjectContext *managedObjectContext;
}
- (NSArray *)items;
- (id)parseXMLAtURL:(NSURL *)url
toObject:(NSString *)aClassName
parseError:(NSError **)error;
#end
It's parsing a xml like this :
<section id="2">
<label>Animaux</label>
<image>Images/Rayons/Bandeau/Animaux.png</image>
<key>Images/Rayons/Bandeau/Animaux.png</key>
<products>
<Product id="21">
<category_id>Chat</category_id>
<label>Aliments pour chat</label>
<price>2.00</price>
</Product>
<Product id="1286">
<category_id>Chat</category_id>
<label>Boite de paté</label>
<price>0.00</price>
</Product>
</products>
</sections>
But it crashes on the line
item = [NSEntityDescription entityForName:className inManagedObjectContext:managedObjectContext];
Anyone can help me?
Thank you
I didn't read all code posted, but I think you're looking for this method:
[NSEntityDescription insertNewObjectForEntityForName:name
inManagedObjectContext:ctx];
+entityForName: only gets the entity description (of type NSEntityDescription) but you're trying to create an object.

UIWebView not showing parsed XML data

I have a tag <AboutUs> (having HTML tags) and I have stored the string part in a local string. Now I want to pass it to my webView to display, but it is showing my string (null).
Here's my code, any help is appreciated:
#class AppDelegate_iPhone;
#interface AboutUsViewController : UIViewController<NSXMLParserDelegate> {
NSMutableString *aboutUsString;
NSString *currentElement;
IBOutlet UIWebView *webView;
AppDelegate_iPhone *appDelegate;
}
#property(nonatomic,retain) NSMutableString *aboutUsString;
#property(nonatomic,retain) IBOutlet UIWebView *webView;
-(id)init;
#end
#implementation AboutUsViewController
#synthesize aboutUsString,webView;
-(id)init{
if(self == [super init]){
aboutUsString = [[NSMutableString alloc]init];
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
NSURL *url = [[NSURL alloc] initWithString:#"http://mobileecommerce.site247365.com/admin/AboutUs.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
AboutUsViewController *parser = [[AboutUsViewController alloc] init];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(#"No Errors");
else
NSLog(#"Error Error Error!!!");
NSLog(#"After Parsing=== = = = = = = = == = = = = %#",appDelegate.TextString);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
currentElement = elementName;
if([currentElement isEqualToString:#"AboutUs"]) {
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if([currentElement isEqualToString:#"AboutUs"]) {
NSMutableString *outputBuilder = [[NSMutableString alloc]init] ;
[outputBuilder appendString:[NSString stringWithFormat:#"%#", self.aboutUsString]];
[outputBuilder appendString:[NSString stringWithFormat:#"%#", string]];
self.aboutUsString = outputBuilder;
[outputBuilder release];
}
else
{
self.aboutUsString = string;
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:#"AboutUs"]) {
[webView setScalesPageToFit:YES];
[webView loadHTMLString:self.aboutUsString baseURL:[NSURL URLWithString:#"http://www.hitchhiker.com/message"]];
NSLog(#"In DID End Element ===== %#",aboutUsString);
appDelegate.TextString = [[NSMutableString alloc]initWithString:aboutUsString];
}
currentElement = #"";
}
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(#"In Document End ========= %#",aboutUsString);
NSLog(#"Appdelgate Text String %#",appDelegate.TextString);
}
- (void)dealloc {
[super dealloc];
[webView release];
}
#end
In your viewDidLoad method, you're creating a new AboutUsViewController which then gets all the NSXMLParser delegate calls. It has its own web view, that one will load the parsed HTML, but you'll never see it because the parsing view controller is never actually visible.
You should set self as the xml parser's delegate instead of creating a new instance.

Simple and concise desktop Cocoa NSXMLParser example?

I would like to look through the elements of a file and when one specific element comes out, output the contents in between the tag.
I tried to follow the example in the Mac Dev entitled Event Driven XML Programming, but it just doesn't finish very clearly. It says to make sure I code the delegates, but it never shows an example. I just want to see a simple example where:
The file is assumed to be a good xml file.
Its path is a URL (or string).
The way the delegate interacts with the parser is explained.
Many tutorials for Cocoa seem to almost teach you to circumvent the delegate classes and make your own IBAction functions so I'm missing the training I think on how to use the delegates properly. Its not clear in the example if I'm supposed to build the delegates in the delegate class or keep them in the class with the parser.
This is based on something I originally wrote for Cut out a part of a long NSString. I copied the NSXMLParserDelegate code from that iOS project into an OS X project. It gets the text from a specific object in a web page.
.h file:
#interface so7576593AppDelegate : NSObject <NSApplicationDelegate, NSXMLParserDelegate> {
NSWindow *window;
IBOutlet NSTextField *textField;
NSMutableString *divCharacters;
BOOL captureCharacters;
}
#property (assign) IBOutlet NSWindow *window;
#end
.m file:
#import "so7576593AppDelegate.h"
#implementation so7576593AppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
captureCharacters = NO;
NSURL *theURL = [NSURL URLWithString:#"http://maxnerios.yolasite.com/"];
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:theURL];
[parser setDelegate:self];
[parser parse];
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
if ([elementName isEqual:#"div"] && [[attributeDict objectForKey:#"id"] isEqual:#"I3_sys_txt"]) {
captureCharacters = YES;
divCharacters = [[NSMutableString alloc] initWithCapacity:500];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if (captureCharacters) {
//from parser:foundCharacters: docs:
//The parser object may send the delegate several parser:foundCharacters: messages to report the characters of an element.
//Because string may be only part of the total character content for the current element, you should append it to the current
//accumulation of characters until the element changes.
[divCharacters appendString:string];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if (captureCharacters) {
captureCharacters = NO;
[textField setStringValue:divCharacters];
[divCharacters release];
}
}
#end
If you click the "Next" link on that page and go onto "Handling XML Elements and Attributes" it will give you an example of how to code the delegates.
Apple provides a Mac example in ImageMap.
There's no difference between NSXMLParser on Mac and iPhone, so reading an iPhone example shouldn't be a problem.
Here is an example of using an NSXMLParser in a custom class that takes in a string of the tag to look for and the xml NSData:
JHXMLParser.h:
#protocol JHXMLParserDelegate <NSObject>
#optional
- (void)acceptParsedData:(NSMutableArray *)parsedData withIdent:(NSString *)ident;
- (void)acceptParsedDebugData:(NSMutableArray *)parsedData withIdent:(NSString *)ident;
#end
#class JHKeyValuePair;
#interface JHXMLParser : NSObject <NSXMLParserDelegate> {
NSString *ident;
#private
id _delegate;
NSMutableArray *_parsedData;
NSString *_key;
NSData *_rawData;
NSXMLParser *_dataParser;
NSString *_previousTag;
NSString *_currentTag;
}
#property (retain, nonatomic) NSString *ident;
- (id)initWithKeyValuePair:(JHKeyValuePair *)kvPair;
- (id)initWithKey:(NSString *)Key andData:(NSData *)data;
// delegate management. The delegate is not retained.
- (id <JHXMLParserDelegate>)delegate;
- (void)setDelegate:(id <JHXMLParserDelegate>)delegate;
- (BOOL)start;
#end
And the JHXMLParser.m:
#import "JHKeyValuePair.h"
#import "JHXMLParser.h"
#implementation JHXMLParser
#synthesize ident;
- (id)init {
if ((self = [super init])) {
ident = [[NSString alloc] init];
}
return self;
}
- (id)initWithKeyValuePair:(JHKeyValuePair *)kvPair {
if ((self = [self init])) {
_key = [kvPair key];
_rawData = [kvPair value];
_dataParser = [[NSXMLParser alloc] initWithData:_rawData];
_dataParser.delegate = self;
}
return self;
}
- (id)initWithKey:(NSString *)key andData:(NSData *)data {
if ((self = [self init])) {
_key = key;
_rawData = data;
_dataParser = [[NSXMLParser alloc] initWithData:_rawData];
_dataParser.delegate = self;
}
return self;
}
- (id <JHXMLParserDelegate>)delegate {
id <JHXMLParserDelegate> d = nil;
if (_delegate) {
d = _delegate;
}
return d;
}
- (void)setDelegate:(id <JHXMLParserDelegate>)delegate {
_delegate = delegate;
}
- (BOOL)start {
return [_dataParser parse];
}
- (void)dealloc {
[_parsedData release];
[_rawData release];
[super dealloc];
}
#pragma mark - NSXMLParser Delegate
- (void)parserDidStartDocument:(NSXMLParser *)parser {
_parsedData = [[NSMutableArray alloc] init];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
_currentTag = elementName;
if ([elementName isEqualToString:_key]) {
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] init];
[_parsedData addObject:tmpDict];
[tmpDict release];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![_previousTag isEqualToString:_currentTag]) {
[[_parsedData lastObject] setObject:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
forKey:_currentTag];
} else {
[[_parsedData lastObject] setObject:[NSString stringWithFormat:#"%#%#",[[_parsedData lastObject] objectForKey:_currentTag], [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]
forKey:_currentTag];
}
if (_previousTag) {
[_previousTag release];
_previousTag = nil;
}
_previousTag = [[NSString alloc] initWithFormat:#"%#", _currentTag];
[pool drain];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (![_previousTag isEqualToString:elementName]) {
[[_parsedData lastObject] setObject:#"" forKey:elementName];
} else {
if (_previousTag) {
[_previousTag release];
_previousTag = nil;
}
_previousTag = [[NSString alloc] initWithFormat:#""];
}
[pool drain];
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
if ([_delegate respondsToSelector:#selector(acceptParsedData:withIdent:)]) {
[_delegate acceptParsedData:_parsedData withIdent:ident];
} else if ([_delegate respondsToSelector:#selector(acceptParsedDebugData:withIdent:)]) {
[_delegate acceptParsedDebugData:_parsedData withIdent:ident];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
#ifdef DEBUG
DEBUGLOG(#"Parser ERROR Occurred: %#", self.ident);
DEBUGLOG(#"ERROR: %#", parseError);
#endif
if ([_delegate respondsToSelector:#selector(acceptParsedData:withIdent:)]) {
[_delegate acceptParsedData:_parsedData withIdent:ident];
} else if ([_delegate respondsToSelector:#selector(acceptParsedDebugData:withIdent:)]) {
[_delegate acceptParsedDebugData:_parsedData withIdent:ident];
}
}
#end
Used like so:
parser = [[[JHXMLParser alloc] initWithKey:#"INFO" andData:connectData] autorelease];
parser.ident = #"INFO";
parser.delegate = self;
[parser start];
Then implement the delegate method:
- (void)acceptParsedData:(NSMutableArray *)parsedData withIdent:(NSString *)ident {
// do stuff here with the parsed data
}