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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:16:13+00:00 2026-05-26T11:16:13+00:00

I have a boolean field called a and two methods void setA(String a) and

  • 0

I have a boolean field called a and two methods void setA(String a) and boolean isA(). I have set @XmlAccessorType(XmlAccessType.NONE) and used @XmlAttribute for the setter.

Because the getter returns a boolean value but the setter expects a string JAX-B just ignores this setter. This is the cause for all kinds of bugs in the code because boolean values are not set correctly and debugging that is very annoying.

Is there a way to tell JAX-B to use the setter? Why is JAX-B confused by the getter method at all, I though using XmlAccessType.NONE prevents all that implicit interpreting?

Plan B would be to let JAX-B fails if such a constellation appears, but how can this be done?

Thankful for any hint 🙂

  • 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-26T11:16:13+00:00Added an answer on May 26, 2026 at 11:16 am

    I would recommend using @XmlAccessType.FIELD as suggested by Kevin combined with an XmlAdapter to get the behaviour you are looking for:

    Root

    To get this example to work with the JAXB-RI I need to make the field of type Boolean. If you are using EclipseLink JAXB (MOXy) then you can make the field boolean.

    package forum7876493;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    public class Root {
    
        @XmlAttribute
        @XmlJavaTypeAdapter(BooleanAdapter.class)
        private Boolean a;
    
        public boolean isA() {
            return a;
        }
    
        public void setA(String s) {
            this.a = "yes".equals(s) || "on".equals(s) || "in".equals(s);
        }
    
    }
    

    BooleanAdapter

    The XmlAdapter is where you can add the logic that you have in your setA(String) method.

    package forum7876493;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class BooleanAdapter extends XmlAdapter<String, Boolean> {
    
        @Override
        public Boolean unmarshal(String s) throws Exception {
            return "yes".equals(s) || "on".equals(s) || "in".equals(s);
        }
    
        @Override
        public String marshal(Boolean b) throws Exception {
            if(b) {
                return "yes";
            }
            return "no";
        }
    
    }
    

    Demo

    package forum7876493;
    
    import java.io.File;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Root.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Root root = (Root) unmarshaller.unmarshal(new File("src/forum7876493/input.xml"));
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(root, System.out);
        }
    
    }
    

    input.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <root a="on"/>
    

    Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <root a="yes"/>
    

    UPDATE

    Alternatively you could introduce a String getter for the a property. You would need to make the isA() method as @XmlTransient:

    package forum7876493;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    
    @XmlRootElement
    public class Root {
    
        private boolean a;
    
        @XmlTransient
        public boolean isA() {
            return a;
        }
    
        @XmlAttribute
        public String getA() {
            if(a) {
                return "yes";
            }
            return "no";
        }
    
        public void setA(String s) {
            this.a = "yes".equals(s) || "on".equals(s) || "in".equals(s);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a boolean field called saved in my database. I want to toggle
Basically I have a field in my table called sex, and it's a boolean.
I have a boolean field called showLabels in a Bean class, and I want
I have a class IPAddressString that has: a private field of type String called
If I have a boolean field like: private static final boolean DEBUG = false;
I have a boolean (BOOL) type field in the SQLite table. In the SubSonic
I have a class that contains a boolean field like this one: public class
I have a strange problem which I can't fix: A field: private boolean[][][] gaps;
I have a Struts (1.3x) ActionForm that has several String and boolean properties/fields, but
Say I have a table with a field called ordernum that denotes the order

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.