I am creating a XML with xmlWriter, I have values in an NSDictionary that has several key/value pairs, however some of these values are [NSNull null], I pass every value into its own NSString.
In my XMLWriter method I have an if statment that checks if this is a particular method call to the db if it is then I would like to create writeAttributes only for the NSString variables that are not equal too [NSNull null].
I am not sure how to dynamically check each variable.. I thought maybe I could do an if statment but thats not going to work because as soon as one of the variables dose not equal [NSNull null] then its going to jump out of this part of the XMLWritter when there might be more variables that are needed for the xml.
This is the failed Idea that I had so you understand what I am trying to do
// Method Params --->
- (NSMutableData *) addMethodParams
{
//allocate serializer (this is using the xmlWriter class)
id <XMLStreamWriter> xmlWriter = [[XMLWriter alloc] init];
[xmlWriter writeStartElement:@"Eng"];
[xmlWriter writeStartElement:@"Parameters"];
[xmlWriter writeStartElement:@"Vars"];
if ([methodName isEqualToString:@"SeriesSearch"]) // name of method currently being requested
{
if ((NSNull *) Series != [NSNull null]) { // if this is null then its jumped
[xmlWriter writeAttribute:@"Code" value:Series];
}
else if ((NSNull *) IDSeries != [NSNull null]){ //if this is !null then it enters the if statement however it then will jump out and not check over any of the other if statments
[xmlWriter writeAttribute:@"ManufacturerID" value:IDSeries];
}
//..
}
[xmlWriter writeEndElement];
[xmlWriter writeEndElement];
[xmlWriter writeEndElement];
So the question is how can I add the variables that have values into my xml the stop the variables that are null? to create I guess a sort of dynamic xml writer.
Usually, when you have to write the same code more than twice (or even more than once), you should think about creating a method or function. So you could write a method like this:
- (void)writeAttribute:(NSString *)name ifNonNullValue:(id)value toWriter:(id<XMLStreamWriter>)writer {
if (value != [NSNull null]) {
[writer writeAttribute:name value:value];
}
}
and use it like this:
[self writeAttribute:@"Code" ifNonNullValue:Series toWriter:xmlWriter];
[self writeAttribute:@"ManufacturerID" ifNonNullValue:IDSeries toWriter:xmlWriter];
...
You could also consider adding a category to XMLWriter
. A category lets you add your own methods to any class. So you could add a category like this:
// XMLWriter+NonNull.h
#import "XMLWriter.h"
@interface XMLWriter (NonNull)
- (void)writeAttribute:(NSString *)name ifNonNullValue:(id)value;
@end
// XMLWriter+NonNull.m
@implementation XMLWriter (NonNull)
- (void)writeAttribute:(NSString *)name ifNonNullValue:(id)value {
if (value != [NSNull null]) {
[self writeAttribute:name value:value];
}
}
@end
and use it like this:
// At top of file
#import "XMLWriter+NonNull.h"
...
[xmlWriter writeAttribute:@"Code" ifNonNullValue:Series];
[xmlWriter writeAttribute:@"ManufacturerID" ifNonNullValue:IDSeries];
...