Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:33:55+00:00 2026-06-08T10:33:55+00:00

I am creating a XML with xmlWriter, I have values in an NSDictionary that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:33:57+00:00Added an answer on June 8, 2026 at 10:33 am

    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];
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml document that I am creating using the loop below and
I am creating an ASHX that returns XML however it expects a path when
I am creating xml file with my own tags and given values and that
I'm creating xml-like mark-up language using System.Xml.XmTextWriter that will be read by a third
I am creating an XML web service that passes an array of custom types.
I am having trouble creating an XML document that contains a default namespace and
I'm creating XML file which will hold couple of values regarding TCP Connection (IP
I'm creating an XML schema that stores information about houses. I want to store
I am creating a Xml like format using XmlWriter . But in the output
Im creating xml string from my RSA key: var keyXmlString = rsaPrivKey.ToXmlString(true); And now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.