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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:28:34+00:00 2026-06-14T02:28:34+00:00

I am using XStream with Annotations to go between my Java objects and XML.

  • 0

I am using XStream with Annotations to go between my Java objects and XML. I am essentially trying to make the XML as minimal as possible, and one the items I want to reduce is the inclusion of boolean values when not necessary. When a boolean value is false I do not want to include it in my xml, since it’s default value is also false.

Is there away to configure XSream to not include values when they are equal to their default values?

Given:

public class Test {

    @XStreamAlias("name")
    @XStreamAsAttribute
    private String name;

    @XStreamAlias("good")
    @XStreamAsAttribute
    private boolean good;

    public Test(String name, boolean good){
        this.name = name;
        this.good = good;
    }

    public static void main(String[] args) {
        XStream stream = new XStream();
        stream.processAnnotations(new Class[] {Test.class});

        Test test1 = new Test("Test", true);
        System.out.println(stream.toXML(test1));
        Test test2 = new Test("Test", false);   
        System.out.println(stream.toXML(test2));    
    }
}

This prints:

<Test name="Test" good="true"/>
<Test name="Test" good="false"/>

I want it to be:

<Test name="Test" good="true"/>
<Test name="Test"/>

Edit:
I noticed BooleanConverter has a method called shouldConvert so I tried overriding that by extending the class. Which did not work, it is never called. The method:

@Override
public boolean shouldConvert(Class type, Object value) {
  System.out.println("Class: "+type+" Value: "+value);
  return (Boolean)value;
}

My variable annotation to set the converter on good

@XStreamConverter(MyBooleanConverter.class)
@XStreamAlias("good")
@XStreamAsAttribute
private boolean good;
  • 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-14T02:28:36+00:00Added an answer on June 14, 2026 at 2:28 am

    You have to write your own BooleanConverter overriding the toString(Objec obj) method.

    public class MyBooleanConverter extends BooleanConverter{
    
        @Override
        public String toString(Object obj) {
            if(((Boolean)obj).equals(false)){
                return null;
            }
            return super.toString(obj);
        }   
    
    }
    

    Then set the Annotation to use the converter on the variable that you want:

    @XStreamConverter(MyBooleanConverter.class)
    @XStreamAlias("good")
    @XStreamAsAttribute
    private boolean good;
    

    That works but requires you to set add the annotation in XStreamConverter every single time. I would prefer to not have to do that, since I want my converter to be used much more frequently than the default. You can use registerConverter to accomplish that. Here is the complete example:

    public class Test {
    
        @XStreamAlias("name")
        @XStreamAsAttribute
        private String name;
    
        @XStreamAlias("good")
        @XStreamAsAttribute
        private boolean good;
    
        public Test(String name, boolean good){
            this.name = name;
            this.good = good;
        }
    
        public static void main(String[] args) {
            XStream stream = new XStream();
            stream.processAnnotations(new Class[] {Test.class});
            stream.registerConverter(new MyBooleanConverter());
    
            Test test1 = new Test("Test", true);
            System.out.println(stream.toXML(test1));
            Test test2 = new Test("Test", false);   
            System.out.println(stream.toXML(test2));    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am planning on using XStream with Java to convert between objects and XML
I'm using XStream to convert java objects to their XML counterparts. One such object
I am trying to parse an XML file to objects using XStream but I
I just started using XStream Annotations, and I am trying to figure out how
This is My Xml generated using xstream <Request> <MSISDN>9900624233</MSISDN> <TRANSID>123456</TRANSID> </Request> i want to
I am using XStream to serialize my Objects to XML format. The formatted xml
We'd like to generate xml using xstream implemented with java. We'd then like to
I am using xstream to de/serialize objects to json. I want to serialize a
I am using XStream to convert XML to objects. I am converting large xml.
I have some Java objects that I want to transform to XML. Some of

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.