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 6723785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:36:37+00:00 2026-05-26T09:36:37+00:00

I import data from a XML file to use it internally. Now there is

  • 0

I import data from a XML file to use it internally. Now there is an uint value, which is (according to the XSD) not required. Now here is the question: How do map this behaviour in my class (it is unclear, if the Value is present or not, but I need to know at runtime)

Basically I see 3 solutions:

Solution 1: Use values of which we know that they are invalid to flag the Value as ‘not-set’:

public class Solution1 {
    private int optionalVal;

    public boolean isSetOptionalVal() {
        return (optionalVal>=0);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
    }

    public void unSetOptionalVal() {
        optionalVal = -1;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

Solution 2: Use the boxed class and set it to null if the value is ‘not-set’:

public class Solution2 {
    private Integer optionalVal;

    public boolean isSetOptionalVal() {
        return (optionalVal!=null);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
    }

    public void unSetOptionalVal() {
        optionalVal = null;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

Solution 3: Use an additional variable that describes the value as ‘not-set’:

public class Solution3 {
    private int optionalVal;
    private boolean optionalValSet;

    public boolean isSetOptionalVal() {
        return (optionalValSet);
    }

    public void setOptionalVal(int val) {
        optionalVal = val;
        optionalValSet = true;
    }

    public void unSetOptionalVal() {
        optionalValSet = false;
    }

    public int optionalVal() {
        if(isSetOptionalVal()) {
            return optionalVal;
        } else {
            return -1;
        }
    }
}

These are my proposals to solve the issue, but I don’t really like any of those.

Solution 1 seems very hacky, maybe there is somewhere a point where I can’t determine the invalid value.

Solution 2 is actually the solution I am using, but I only need the additional information for some of the memeber variables, so I have either to use some variables as boxed types and some as primitive (which seems inconsistent) or I have always to use the boxed types (which I don’t really like).

Solution 3 seems to be the cleanest, but here I am worried, that at some place the bool isn’t set correctly, which would be a hard to find error (I already have a lot of code, and found the problem, that some elements are not set in the XML just recently)

So…what would you prefer as a solution to solve the “Optional Value”-problem – is there maybe an even better solution?
How is this problem generally handled?

  • 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-05-26T09:36:38+00:00Added an answer on May 26, 2026 at 9:36 am

    I’d choose option 2, using the Integer class, and leave the conversion between int and Integer to autoboxing. The advantage of this approach is that it keeps everything concerned with your optional value in a single variable.

    The first option is a magic value, and if the unused value becomes a used value later, it becomes a nightmare to maintain.

    The third option means having to keep track of both the int and the boolean that keeps track of the question whether it is used. If you’re going to do this, consider making it a class in and of itself… but then you might as well use Integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program is now still running to import data from a log file into
Is there a way to import data from a JSON file into R? More
I'm trying to use Nokogiri to grab some data from an XML file, then
In this situation I am trying to perform a data import from an XML
I will be doing an occiasional import from XML into core data. I have
I import data from a TSV file with SQL Server 2008. null is replaced
I need to import data from *.dmp file say mydump.dmp. What I'm doing is:
I have simple SSIS package where I import data from flat file into SQL
I need to make an excel file which would fetch data from connection to
I have an XML file and I want to retrieve data from it so

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.