Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing DelegateProtocoll #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The key ideas:

1 XML elements map to key names in the dictionary
2 Each element corresponds to a child dictionary
3 Attribute key-value pairs are added to the element’s child dictionary
4 Strings from text nodes are assigned to the child dictionary’s “text” key
5 If an element name is encountered multiple times, the value of the element is set to an array of children dictionaries
4 changes: 2 additions & 2 deletions XMLReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#import <Foundation/Foundation.h>


@interface XMLReader : NSObject
{
@interface XMLReader : NSObject <NSXMLParserDelegate> {
NSMutableArray *dictionaryStack;
NSMutableString *textInProgress;
NSError **errorPointer;
Expand Down
60 changes: 30 additions & 30 deletions XMLReader.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,41 @@ @implementation XMLReader
#pragma mark -
#pragma mark Public methods

+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error
{
+ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error {
XMLReader *reader = [[XMLReader alloc] initWithError:error];
NSDictionary *rootDictionary = [reader objectWithData:data];
[reader release];
return rootDictionary;
}

+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error
{
+ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error {
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [XMLReader dictionaryForXMLData:data error:error];
}

#pragma mark -
#pragma mark Parsing

- (id)initWithError:(NSError **)error
{
if (self = [super init])
{
- (id)initWithError:(NSError **)error {
if (self == [super init]) {
errorPointer = error;
}
return self;
}

- (void)dealloc
{
- (void)dealloc {
[dictionaryStack release];
[textInProgress release];
[super dealloc];
}

- (NSDictionary *)objectWithData:(NSData *)data
{
- (NSDictionary *)objectWithData:(NSData *)data {
// Clear out any old data
[dictionaryStack release];
[textInProgress release];
Expand All @@ -70,20 +70,23 @@ - (NSDictionary *)objectWithData:(NSData *)data
BOOL success = [parser parse];

// Return the stack's root dictionary on success
if (success)
{
NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
if (success){

NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];

[parser release];
return resultDict;
}


[parser release];
return nil;
}

#pragma mark -
#pragma mark NSXMLParserDelegate methods

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
// Get the dictionary for the current level in the stack
NSMutableDictionary *parentDict = [dictionaryStack lastObject];

Expand All @@ -93,8 +96,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam

// If there's already an item for this key, it means we need to create an array
id existingValue = [parentDict objectForKey:elementName];
if (existingValue)
{
if (existingValue) {
NSMutableArray *array = nil;
if ([existingValue isKindOfClass:[NSMutableArray class]])
{
Expand All @@ -114,8 +116,8 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
// Add the new child dictionary to the array
[array addObject:childDict];
}
else
{
else {
// No existing value, so update the dictionary
[parentDict setObject:childDict forKey:elementName];
}
Expand All @@ -124,14 +126,14 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
[dictionaryStack addObject:childDict];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
// Update the parent dict with text info
NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];

// Set the text property
if ([textInProgress length] > 0)
{
if ([textInProgress length] > 0) {
[dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];

// Reset the text
Expand All @@ -143,14 +145,12 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names
[dictionaryStack removeLastObject];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
// Build the text value
[textInProgress appendString:string];
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
// Set the error pointer to the parser's error object
*errorPointer = parseError;
}
Expand Down