Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Newline at end of files #8

Closed
wants to merge 9 commits into from

2 participants

Commits on Oct 18, 2011
  1. Compatible with Xcode 4.2 and ARC.

    t3443t authored committed
  2. Compatible with Xcode 4.2 and ARC.

    t3443t authored committed
Commits on Oct 19, 2011
  1. Simplification of header; assuming LLVM 3.0 due to ARC requirement. N…

    authored
    …ew -fullDescription doesn't truncate values.
  2. Updated README with ARC info.

    authored
Commits on Oct 26, 2011
Commits on Feb 4, 2012
Commits on Feb 25, 2012
  1. Updated README.

    authored
Commits on Apr 30, 2012
  1. @AquaGeek

    Add newline at end of files

    AquaGeek authored
This page is out of date. Refresh to see the latest.
View
23 README.md
@@ -3,9 +3,30 @@
SMXMLDocument is a very handy lightweight XML parser for iOS.
+In brief:
+
+ // create a new SMXMLDocument with the contents of sample.xml
+ SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
+
+ // Pull out the <books> node
+ SMXMLElement *books = [document.root childNamed:@"books"];
+
+ // Look through <books> children of type <book>
+ for (SMXMLElement *book in [books childrenNamed:@"book"]) {
+
+ // demonstrate common cases of extracting XML data
+ NSString *isbn = [book attributeNamed:@"isbn"]; // XML attribute
+ NSString *title = [book valueWithPath:@"title"]; // child node value
+
+ // show off some KVC magic
+ NSArray *authors = [[book childNamed:@"authors"].children valueForKey:@"value"];
+
+ // do interesting things...
+ }
+
More info in the blog post:
http://nfarina.com/post/2843708636/a-lightweight-xml-parser-for-ios
### ARC Support
-If you are including SMXMLDocument in a project that has [Automatic Reference Counting (ARC)](http://clang.llvm.org/docs/AutomaticReferenceCounting.html) enabled, you will need to set the `-fno-objc-arc` compiler flag for our source. To do this in Xcode, go to your active target and select the "Build Phases" tab. In the "Compiler Flags" column, set `-fno-objc-arc` for `SMXMLDocument.m`.
+This branch supports (and requires) [Automatic Reference Counting (ARC)](http://clang.llvm.org/docs/AutomaticReferenceCounting.html)
View
24 SMXMLDocument.h
@@ -28,23 +28,15 @@ extern NSString *const SMXMLDocumentErrorDomain;
@class SMXMLDocument;
-@interface SMXMLElement : NSObject<NSXMLParserDelegate> {
-@private
- SMXMLDocument *document; // nonretained
- SMXMLElement *parent; // nonretained
- NSString *name;
- NSMutableString *value;
- NSMutableArray *children;
- NSDictionary *attributes;
-}
+@interface SMXMLElement : NSObject<NSXMLParserDelegate>
-@property (nonatomic, assign) SMXMLDocument *document;
-@property (nonatomic, assign) SMXMLElement *parent;
+@property (nonatomic, weak) SMXMLDocument *document;
+@property (nonatomic, weak) SMXMLElement *parent;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) NSString *value;
@property (nonatomic, retain) NSArray *children;
-@property (nonatomic, readonly) SMXMLElement *firstChild, *lastChild;
@property (nonatomic, retain) NSDictionary *attributes;
+@property (nonatomic, readonly) SMXMLElement *firstChild, *lastChild;
- (id)initWithDocument:(SMXMLDocument *)document;
- (SMXMLElement *)childNamed:(NSString *)name;
@@ -56,17 +48,15 @@ extern NSString *const SMXMLDocumentErrorDomain;
@end
-@interface SMXMLDocument : NSObject<NSXMLParserDelegate> {
-@private
- SMXMLElement *root;
- NSError *error;
-}
+@interface SMXMLDocument : NSObject<NSXMLParserDelegate>
@property (nonatomic, retain) SMXMLElement *root;
@property (nonatomic, retain) NSError *error;
- (id)initWithData:(NSData *)data error:(NSError **)outError;
+- (NSString *)fullDescription; // like -description, this writes the document out to an XML string, but doesn't truncate the node values.
+
+ (SMXMLDocument *)documentWithData:(NSData *)data error:(NSError **)outError;
@end
View
55 SMXMLDocument.m
@@ -22,17 +22,7 @@ - (id)initWithDocument:(SMXMLDocument *)aDocument {
return self;
}
-- (void)dealloc {
- self.document = nil;
- self.parent = nil;
- self.name = nil;
- self.value = nil;
- self.children = nil;
- self.attributes = nil;
- [super dealloc];
-}
-
-- (NSString *)descriptionWithIndent:(NSString *)indent {
+- (NSString *)descriptionWithIndent:(NSString *)indent truncatedValues:(BOOL)truncated {
NSMutableString *s = [NSMutableString string];
[s appendFormat:@"%@<%@", indent, name];
@@ -40,26 +30,26 @@ - (NSString *)descriptionWithIndent:(NSString *)indent {
for (NSString *attribute in attributes)
[s appendFormat:@" %@=\"%@\"", attribute, [attributes objectForKey:attribute]];
- NSString *trimVal = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
-
- if (trimVal.length > 25)
- trimVal = [NSString stringWithFormat:@"%@", [trimVal substringToIndex:25]];
+ NSString *valueOrTrimmed = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
+
+ if (truncated && valueOrTrimmed.length > 25)
+ valueOrTrimmed = [NSString stringWithFormat:@"%@", [valueOrTrimmed substringToIndex:25]];
if (children.count) {
[s appendString:@">\n"];
NSString *childIndent = [indent stringByAppendingString:@" "];
- if (trimVal.length)
- [s appendFormat:@"%@%@\n", childIndent, trimVal];
+ if (valueOrTrimmed.length)
+ [s appendFormat:@"%@%@\n", childIndent, valueOrTrimmed];
for (SMXMLElement *child in children)
- [s appendFormat:@"%@\n", [child descriptionWithIndent:childIndent]];
+ [s appendFormat:@"%@\n", [child descriptionWithIndent:childIndent truncatedValues:truncated]];
[s appendFormat:@"%@</%@>", indent, name];
}
- else if (trimVal.length) {
- [s appendFormat:@">%@</%@>", trimVal, name];
+ else if (valueOrTrimmed.length) {
+ [s appendFormat:@">%@</%@>", valueOrTrimmed, name];
}
else [s appendString:@"/>"];
@@ -67,7 +57,7 @@ - (NSString *)descriptionWithIndent:(NSString *)indent {
}
- (NSString *)description {
- return [self descriptionWithIndent:@""];
+ return [self descriptionWithIndent:@"" truncatedValues:YES];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
@@ -81,7 +71,7 @@ - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
- SMXMLElement *child = [[[SMXMLElement alloc] initWithDocument:self.document] autorelease];
+ SMXMLElement *child = [[SMXMLElement alloc] initWithDocument:self.document];
child.parent = self;
child.name = elementName;
child.attributes = attributeDict;
@@ -114,7 +104,7 @@ - (NSArray *)childrenNamed:(NSString *)nodeName {
for (SMXMLElement *child in children)
if ([child.name isEqual:nodeName])
[array addObject:child];
- return [[array copy] autorelease];
+ return [array copy];
}
- (SMXMLElement *)childWithAttribute:(NSString *)attributeName value:(NSString *)attributeValue {
@@ -153,7 +143,7 @@ @implementation SMXMLDocument
- (id)initWithData:(NSData *)data error:(NSError **)outError {
self = [super init];
if (self) {
- NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:data] autorelease];
+ NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldReportNamespacePrefixes:YES];
@@ -163,26 +153,21 @@ - (id)initWithData:(NSData *)data error:(NSError **)outError {
if (self.error) {
if (outError)
*outError = self.error;
- [self release];
return nil;
}
+ else if (outError)
+ *outError = nil;
}
return self;
}
-- (void)dealloc {
- self.root = nil;
- self.error = nil;
- [super dealloc];
-}
-
+ (SMXMLDocument *)documentWithData:(NSData *)data error:(NSError **)outError {
- return [[[SMXMLDocument alloc] initWithData:data error:outError] autorelease];
+ return [[SMXMLDocument alloc] initWithData:data error:outError];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
- self.root = [[[SMXMLElement alloc] initWithDocument:self] autorelease];
+ self.root = [[SMXMLElement alloc] initWithDocument:self];
root.name = elementName;
root.attributes = attributeDict;
[parser setDelegate:root];
@@ -196,4 +181,8 @@ - (NSString *)description {
return root.description;
}
+- (NSString *)fullDescription {
+ return [root descriptionWithIndent:@"" truncatedValues:NO];
+}
+
@end
View
9 XMLDocumentSamples/AppDelegate.m
@@ -10,8 +10,15 @@ - (void) applicationDidFinishLaunching:(UIApplication *)application {
NSData *data = [NSData dataWithContentsOfFile:sampleXML];
// create a new SMXMLDocument with the contents of sample.xml
- SMXMLDocument *document = [SMXMLDocument documentWithData:data error:NULL];
+ NSError *error;
+ SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];
+ // check for errors
+ if (error) {
+ NSLog(@"Error while parsing the document: %@", error);
+ return;
+ }
+
// demonstrate -description of document/element classes
NSLog(@"Document:\n %@", document);
View
15 XMLDocumentSamples/XMLDocumentSamples.xcodeproj/project.pbxproj
@@ -3,13 +3,13 @@
archiveVersion = 1;
classes = {
};
- objectVersion = 45;
+ objectVersion = 46;
objects = {
/* Begin PBXBuildFile section */
0173BC9C12E765430037CA90 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BC9B12E765430037CA90 /* AppDelegate.m */; };
- 0173BCAA12E7656F0037CA90 /* SMXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BCA812E7656F0037CA90 /* SMXMLDocument.m */; };
0173BCC312E766160037CA90 /* sample.xml in Resources */ = {isa = PBXBuildFile; fileRef = 0173BCC212E766160037CA90 /* sample.xml */; };
+ 01B0AE9D144DE6A00020AF67 /* SMXMLDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = 0173BCA812E7656F0037CA90 /* SMXMLDocument.m */; };
1D60589B0D05DD56006BFB54 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; };
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D30AB110D05D00D00671497 /* Foundation.framework */; };
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1DF5F4DF0D08C38300B7A737 /* UIKit.framework */; };
@@ -121,8 +121,11 @@
/* Begin PBXProject section */
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
+ attributes = {
+ LastUpgradeCheck = 0430;
+ };
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "XMLDocumentSamples" */;
- compatibilityVersion = "Xcode 3.1";
+ compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
@@ -158,7 +161,7 @@
files = (
1D60589B0D05DD56006BFB54 /* main.m in Sources */,
0173BC9C12E765430037CA90 /* AppDelegate.m in Sources */,
- 0173BCAA12E7656F0037CA90 /* SMXMLDocument.m in Sources */,
+ 01B0AE9D144DE6A00020AF67 /* SMXMLDocument.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -169,6 +172,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
+ CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
@@ -183,6 +187,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
+ CLANG_ENABLE_OBJC_ARC = YES;
COPY_PHASE_STRIP = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = XMLDocumentSamples_Prefix.pch;
@@ -201,7 +206,6 @@
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
- PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
@@ -217,7 +221,6 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
- PREBINDING = NO;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
};
View
8 XMLDocumentSamples/main.m
@@ -1,8 +1,6 @@
int main(int argc, char *argv[]) {
-
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
- [pool release];
- return retVal;
+ @autoreleasepool {
+ return UIApplicationMain(argc, argv, nil, @"AppDelegate");
+ }
}
Something went wrong with that request. Please try again.