Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

JavaBean XMLDecoder not persisting default values

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
adg
Asked:
adg
  • 9
  • 8
1 Solution
 
CEHJCommented:
Assign defaults in the empty ctor
0
 
adgAuthor Commented:
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
 
adgAuthor Commented:
How does it know what the default values are anyway?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
>>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
 
adgAuthor Commented:
>> 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
 
adgAuthor Commented:
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
 
CEHJCommented:
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
 
CEHJCommented:
Another thing - make sure that none of the object's members is declared static unless you have a good reason for doing so
0
 
adgAuthor Commented:
>> 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
 
CEHJCommented:
>>. 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
 
adgAuthor Commented:
>> 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
 
CEHJCommented:
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
 
CEHJCommented:
8-)

Sorry about the dispersed class members btw ;-)
0
 
adgAuthor Commented:
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
 
CEHJCommented:
>>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
 
adgAuthor Commented:
>> 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
 
CEHJCommented:
Oh OK - i see
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now