Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JavaBean XMLDecoder not persisting default values

Posted on 2004-10-09
17
Medium Priority
?
337 Views
Last Modified: 2008-02-01
I have a very simple javabean that I'm persisting using XMLDecoder but it doesn;t persist default values. In this case I would like all the properties persisted regardless. Is there an easy way to make it do that?

Thanks!
0
Comment
Question by:adg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12265643
Assign defaults in the empty ctor
0
 
LVL 3

Author Comment

by:adg
ID: 12265702
Thats better but still not right. Here is my constructor. Now the Strings are always coming out but the boolean and int are not!

Here is my constructor

    public MyBean()
    {
        selected = false;
        condition = "";
        action = NO_ACTION;
        comment = "";      
    }
0
 
LVL 3

Author Comment

by:adg
ID: 12265709
How does it know what the default values are anyway?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 12265716
>>but the boolean and int are not!

... meaning 'selected' and 'action' i take it? What is the value of 'NO_ACTION'? And what output values are you getting? (In order for 'selected' to be 'not coming out' it would have to be coming out as true ;-))
0
 
LVL 3

Author Comment

by:adg
ID: 12265730
>> meaning 'selected' and 'action' i take it?
Right!

>> 'selected' to be 'not coming out' it would have to be coming out as true
It seems to be the other way around. When it is true it does show up, when it is false it does not.  I assume that's because false is the default.

>> What is the value of 'NO_ACTION'
It is supposed to be 103. That is why I asked how it knows what the deault it. I assumed the default would be zero and that it would always show because it is not zero. But that's not what I'm seeing.

Thanks for looking at this!


0
 
LVL 3

Author Comment

by:adg
ID: 12265758
Here is the output:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_05" class="java.beans.XMLDecoder">
 <object class="test.MyBean">
  <void property="comment">
   <null/>
  </void>
  <void property="condition">
   <null/>
  </void>
  <void property="selected">
   <boolean>true</boolean>
  </void>
 </object>
 <object class="test.MyBean">
  <void property="comment">
   <null/>
  </void>
  <void property="condition">
   <null/>
  </void>
 </object>
 <object class="test.MyBean">
  <void property="comment">
   <null/>
  </void>
  <void property="condition">
   <null/>
  </void>
  <void property="selected">
   <boolean>true</boolean>
  </void>
 </object>
 <object class="test.MyBean">
  <void property="comment">
   <null/>
  </void>
  <void property="condition">
   <string>sssss</string>
  </void>
 </object>
</java>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12265772
Defaults are either provided by you, or the JVM. If the latter, all object refs are made made null and all primitives made 0 (or false)

1. Are you setting 'selected' to true later in the code?
2. Is 'action' declared as transient?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12265842
Another thing - make sure that none of the object's members is declared static unless you have a good reason for doing so
0
 
LVL 3

Author Comment

by:adg
ID: 12267645
>> 1. Are you setting 'selected' to true later in the code
Yes, when I set it to true then it shows up in the XML. When I set it to false, or don't set it at all then it doesn't show up.

>> 2. Is 'action' declared as transient?
No

>> make sure that none of the object's members is declared static unless you have a good reason for doing so
NO_ACTION is declared as final static but I think I have a good reason for doing so since I'm using is as a constant.

There has got to be a way to turn off the mechanism that prevents default values from being persisted.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12267664
>>. When I set it to false, or don't set it at all then it doesn't show up.

That's because a default value can be used on readObject
0
 
LVL 3

Author Comment

by:adg
ID: 12267772
>> That's because a default value can be used on readObject
I've just looked at readObject in the XMLDecoder class but I don;t understand what you're referring to. I'm trying to make the value show up in the XML even if it is false. :*)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12267965
Have a look at this example:


//SNIP ===========================================

import java.beans.XMLEncoder;

public class Bean {

      private String condition;

      public void setCondition(String condition) {
            this.condition = condition;
      }

      public String getCondition() {
            return condition;
      }

      private int action;

      public void setAction(int action) {
            this.action = action;
      }

      public int getAction() {
            return action;
      }

      private boolean selected;

      public void setSelected(boolean selected) {
            this.selected = selected;
      }

      public boolean getSelected() {
            return selected;
      }

      public static void main(String[] args) {

            Bean bean1 = new Bean();

            Bean bean2 = new Bean();
            bean2.setSelected(true);

            Bean bean3 = new Bean();
            bean3.setSelected(true);
            bean3.setCondition("good");

            Bean bean4 = new Bean();
            bean4.setSelected(true);
            bean4.setCondition("good");
            bean4.setAction(10);

            try {
                  XMLEncoder xmlEnc = new XMLEncoder(new java.io.FileOutputStream("beans.xml"));
                  xmlEnc.writeObject(bean1);
                  xmlEnc.writeObject(bean2);
                  xmlEnc.writeObject(bean3);
                  xmlEnc.writeObject(bean4);
                  xmlEnc.close();
            }
            catch (Exception e) {
                  e.printStackTrace();
            }

      }

}

//SNIP ===========================================

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.1_01" class="java.beans.XMLDecoder">
 <object class="Bean"/>
 <object class="Bean">
  <void property="selected">
   <boolean>true</boolean>
  </void>
 </object>
 <object class="Bean">
  <void property="condition">
   <string>good</string>
  </void>
  <void property="selected">
   <boolean>true</boolean>
  </void>
 </object>
 <object class="Bean">
  <void property="action">
   <int>10</int>
  </void>
  <void property="condition">
   <string>good</string>
  </void>
  <void property="selected">
   <boolean>true</boolean>
  </void>
 </object>
</java>
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12268101
8-)

Sorry about the dispersed class members btw ;-)
0
 
LVL 3

Author Comment

by:adg
ID: 12268139
You can still see the problem in your example - the first bean has no properties in the XML.

After working with your comments and giving it some more thought I realized a workaround. Since Strings had the behavior I wanted, I just made all the values Strings. I have to convert back and forth from boolean and int to String but it makes the XML look much better. Thanks for your help!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12268151
>>You can still see the problem in your example - the first bean has no properties in the XML.

There's no problem. As i mentioned, the reason that it's empty is that the default values are used.
0
 
LVL 3

Author Comment

by:adg
ID: 12268318
>> There's no problem. As i mentioned, the reason that it's empty is that the default values are used.

Yes, that was the problem. That is why I asked the question. I wanted the values in the XML REGARDLESS OF THE DEFAULTS. That was the whole point of my question!

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12268321
Oh OK - i see
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question