Database Reference
In-Depth Information
}
- (IBAction)sendNews:(id)sender {
NSLog(@"By Format: %@",
[self constructNewsItemXMLByFormatWithSubject:
@"This is a test subject"
body:@"Buggy whips are cool, aren't they?"]);
}
If we hook the sendNews IBAction up to a new bar button item and run the app, we can
see that perfectly reasonable XML is produced:
By Format: <newsitem postdate="2011-07-26" posttime="10:42"> \
<subject>This is a test subject<subject>\
<body>Buggy whips are cool, aren't they?</body></newsitem>
Let's compare this with the corresponding code, done using DOM construction tech-
niques ( Example 4-8 ).
Example 4-8. Constructing XML by building a DOM
#import "DDXMLElementAdditions.h"
.
.
.
-(NSString *) constructNewsItemXMLByDOMWithSubject:
(NSString *) subject body:(NSString *) body {
NSDate *now = [NSDate date];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"YYYY-MM-dd"];
NSDateFormatter *tf = [NSDateFormatter new];
[tf setDateFormat:@"hh:mm"];
DDXMLElement *postdate =
[DDXMLElement attributeWithName:@"postdate"
stringValue:[df stringFromDate:now]];
DDXMLElement *posttime =
[DDXMLElement attributeWithName:@"posttime"
stringValue:[tf stringFromDate:now]];
NSArray *attributes =
[NSArray arrayWithObjects:postdate, posttime, nil];
DDXMLElement *subjectNode =
[DDXMLElement elementWithName:@"subject"
stringValue:subject];
DDXMLElement *bodyNode =
[DDXMLElement elementWithName:@"body"
stringValue:body];
NSArray *children =
[NSArray arrayWithObjects:subjectNode, bodyNode, nil];
DDXMLElement *doc =
[DDXMLElement elementWithName:@"newsitem"
children:children
attributes:attributes];
NSString *xml = [doc XMLString];
return xml;
}
 
Search WWH ::




Custom Search