Solved

JavaBean XMLDecoder not persisting default values

Posted on 2004-10-09
17
335 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 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

622 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