Solved

JavaBean XMLDecoder not persisting default values

Posted on 2004-10-09
17
330 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
  • 9
  • 8
17 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 48
wordlen challenge 3 47
Running Jira on Raspberry PI 2? 3 137
backtracking recursion  code 19 40
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now