*help* my XMLEncoder.writeObject is not doing all fields

Discussion in 'Java' started by timasmith@hotmail.com, Jun 22, 2006.

  1. Guest

    I have a regular class, implements serializable, private variables with
    getter/setter.

    Nothing prints - despite being populated - ahhh driving me nuts

    The plot thickens when I use hardcoded strings to set the properties
    i.e. instead of cp.getUserName() I used "username"

    If I put println in the method it proves there is data...

    public void saveConnection(ConnProperty cp)
    ConnProperty conn = new ConnProperty();
    conn.setUsername(cp.getUserName());
    conn.setDriverClass(cp.getDriver());
    conn.setPassword(cp.getPassword);
    conn.setUrl(cp.getUrl());
    ConnectionList.setConfigFileName("test.xml");
    ConnectionList list =new ConnectionList();
    list.add(conn);
    SerializeUtility.serializeXML(new File(filename),
    list.elements());

    public static void serializeXML(File file, Enumeration e1) throws
    IOException {
    XMLEncoder encoder = null;
    try
    {
    encoder = new XMLEncoder(
    new BufferedOutputStream(
    new FileOutputStream(file)));
    while (e1.hasMoreElements()) {
    encoder.writeObject(e1.nextElement());
    }
    } catch (Exception ex) {
    Debug.LogException("SerializeUtility", ex);
    }
    finally
    {
    if (encoder != null) {
    encoder.close();
    }
    }
    }



    package com.osframework.datalibrary.common;

    import java.io.Serializable;

    public class ConnProperty implements Serializable {

    private static final long serialVersionUID = 0L;
    /**
    * False for MS Access, True for almost all other databases.
    */
    private boolean connectToURL = true;

    /**
    * URL to connect to remote database (or MS Access local file)
    * e.g. jdbc:eek:racle:thin:mad:localhost:1521:prod1
    */
    private String url = "jdbc:eek:dbc:DRIVER={Microsoft Access Driver
    (*.mdb)};DBQ=osrmt.mdb";

    /**
    * Driver implementation - class name - must be in the classpath!
    */
    private String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";

    /**
    * Database connections of the same environment name
    * will access URLs in this order
    */
    private int accessSequence = 0;

    private String username = "admin";

    private String password = "";

    public int getAccessSequence() {
    return accessSequence;
    }

    public void setAccessSequence(int accessSequence) {
    this.accessSequence = accessSequence;
    }



    public boolean isConnectToURL() {
    return connectToURL;
    }

    public void setConnectToURL(boolean connectToURL) {
    this.connectToURL = connectToURL;
    }

    public String getDriverClass() {
    return driverClass;
    }

    public void setDriverClass(String driverClass) {
    this.driverClass = driverClass;
    }


    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getUrl() {
    return url;
    }

    public void setUrl(String url) {
    this.url = url;
    }

    public String getUsername() {
    return username;
    }

    public void setUsername(String username) {
    this.username = username;
    }



    }
     
    , Jun 22, 2006
    #1
    1. Advertising

  2. Not sure when you say "nothing prints" if you mean the file is empty, or
    has an empty XML element. The former implies I/O error -- any errors in
    your log? The latter can be a number of things.

    But here are some points to consider.

    First, this isn't real test code since it calls getDriver() instead of
    getDriverClass(). Always post the real code you ran.

    Second, I don't think you can write more than one object. The output is
    a complete XML root and only one root is allowed per file. If you want
    the file to contain a list of ConnProperty objects, just write the list
    itself. The Encoder will dump the list in XML and all it's parts. Very
    neat and simple. No loop on your part.

    Third, why are you copying the object before serializing? Just curious.

    Fourth, don't forget that if your object only has default data, nothing
    will be written except an element representing that there is such an
    object. That's because the encoder doesn't write any data that the
    default constructor can take care of. So a default object is not a good
    test case.

    Hope this helps.

    wrote:
    > I have a regular class, implements serializable, private variables with
    > getter/setter.
    >
    > Nothing prints - despite being populated - ahhh driving me nuts
    >
    > The plot thickens when I use hardcoded strings to set the properties
    > i.e. instead of cp.getUserName() I used "username"
    >
    > If I put println in the method it proves there is data...
    >
    > public void saveConnection(ConnProperty cp)
    > ConnProperty conn = new ConnProperty();
    > conn.setUsername(cp.getUserName());
    > conn.setDriverClass(cp.getDriver());
    > conn.setPassword(cp.getPassword);
    > conn.setUrl(cp.getUrl());
    > ConnectionList.setConfigFileName("test.xml");
    > ConnectionList list =new ConnectionList();
    > list.add(conn);
    > SerializeUtility.serializeXML(new File(filename),
    > list.elements());
    >
    > public static void serializeXML(File file, Enumeration e1) throws
    > IOException {
    > XMLEncoder encoder = null;
    > try
    > {
    > encoder = new XMLEncoder(
    > new BufferedOutputStream(
    > new FileOutputStream(file)));
    > while (e1.hasMoreElements()) {
    > encoder.writeObject(e1.nextElement());
    > }
    > } catch (Exception ex) {
    > Debug.LogException("SerializeUtility", ex);
    > }
    > finally
    > {
    > if (encoder != null) {
    > encoder.close();
    > }
    > }
    > }
    >
    >
    >
    > package com.osframework.datalibrary.common;
    >
    > import java.io.Serializable;
    >
    > public class ConnProperty implements Serializable {
    >
    > private static final long serialVersionUID = 0L;
    > /**
    > * False for MS Access, True for almost all other databases.
    > */
    > private boolean connectToURL = true;
    >
    > /**
    > * URL to connect to remote database (or MS Access local file)
    > * e.g. jdbc:eek:racle:thin:mad:localhost:1521:prod1
    > */
    > private String url = "jdbc:eek:dbc:DRIVER={Microsoft Access Driver
    > (*.mdb)};DBQ=osrmt.mdb";
    >
    > /**
    > * Driver implementation - class name - must be in the classpath!
    > */
    > private String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
    >
    > /**
    > * Database connections of the same environment name
    > * will access URLs in this order
    > */
    > private int accessSequence = 0;
    >
    > private String username = "admin";
    >
    > private String password = "";
    >
    > public int getAccessSequence() {
    > return accessSequence;
    > }
    >
    > public void setAccessSequence(int accessSequence) {
    > this.accessSequence = accessSequence;
    > }
    >
    >
    >
    > public boolean isConnectToURL() {
    > return connectToURL;
    > }
    >
    > public void setConnectToURL(boolean connectToURL) {
    > this.connectToURL = connectToURL;
    > }
    >
    > public String getDriverClass() {
    > return driverClass;
    > }
    >
    > public void setDriverClass(String driverClass) {
    > this.driverClass = driverClass;
    > }
    >
    >
    > public String getPassword() {
    > return password;
    > }
    >
    > public void setPassword(String password) {
    > this.password = password;
    > }
    >
    > public String getUrl() {
    > return url;
    > }
    >
    > public void setUrl(String url) {
    > this.url = url;
    > }
    >
    > public String getUsername() {
    > return username;
    > }
    >
    > public void setUsername(String username) {
    > this.username = username;
    > }
    >
    >
    >
    > }
    >
     
    Chris Riesbeck, Jun 22, 2006
    #2
    1. Advertising

  3. Guest

    ooooohhhhhh mannnn

    I've been banging my head against the wall for 3 hours - and all the
    time it doesnt write it because my private variables are initialized to
    that value????

    Well I say that is absurd ... so I persist the object to XML, later
    change the default value of my *private* variables - and later
    XML-->Object = wrong values

    icky

    Obviously the workaround is initiliaze nothing but integers and
    boolean.

    anyway - THANKS!



    Chris Riesbeck wrote:
    > Not sure when you say "nothing prints" if you mean the file is empty, or
    > has an empty XML element. The former implies I/O error -- any errors in
    > your log? The latter can be a number of things.
    >
    > But here are some points to consider.
    >
    > First, this isn't real test code since it calls getDriver() instead of
    > getDriverClass(). Always post the real code you ran.
    >
    > Second, I don't think you can write more than one object. The output is
    > a complete XML root and only one root is allowed per file. If you want
    > the file to contain a list of ConnProperty objects, just write the list
    > itself. The Encoder will dump the list in XML and all it's parts. Very
    > neat and simple. No loop on your part.
    >
    > Third, why are you copying the object before serializing? Just curious.
    >
    > Fourth, don't forget that if your object only has default data, nothing
    > will be written except an element representing that there is such an
    > object. That's because the encoder doesn't write any data that the
    > default constructor can take care of. So a default object is not a good
    > test case.
    >
    > Hope this helps.
    >
    > wrote:
    > > I have a regular class, implements serializable, private variables with
    > > getter/setter.
    > >
    > > Nothing prints - despite being populated - ahhh driving me nuts
    > >
    > > The plot thickens when I use hardcoded strings to set the properties
    > > i.e. instead of cp.getUserName() I used "username"
    > >
    > > If I put println in the method it proves there is data...
    > >
    > > public void saveConnection(ConnProperty cp)
    > > ConnProperty conn = new ConnProperty();
    > > conn.setUsername(cp.getUserName());
    > > conn.setDriverClass(cp.getDriver());
    > > conn.setPassword(cp.getPassword);
    > > conn.setUrl(cp.getUrl());
    > > ConnectionList.setConfigFileName("test.xml");
    > > ConnectionList list =new ConnectionList();
    > > list.add(conn);
    > > SerializeUtility.serializeXML(new File(filename),
    > > list.elements());
    > >
    > > public static void serializeXML(File file, Enumeration e1) throws
    > > IOException {
    > > XMLEncoder encoder = null;
    > > try
    > > {
    > > encoder = new XMLEncoder(
    > > new BufferedOutputStream(
    > > new FileOutputStream(file)));
    > > while (e1.hasMoreElements()) {
    > > encoder.writeObject(e1.nextElement());
    > > }
    > > } catch (Exception ex) {
    > > Debug.LogException("SerializeUtility", ex);
    > > }
    > > finally
    > > {
    > > if (encoder != null) {
    > > encoder.close();
    > > }
    > > }
    > > }
    > >
    > >
    > >
    > > package com.osframework.datalibrary.common;
    > >
    > > import java.io.Serializable;
    > >
    > > public class ConnProperty implements Serializable {
    > >
    > > private static final long serialVersionUID = 0L;
    > > /**
    > > * False for MS Access, True for almost all other databases.
    > > */
    > > private boolean connectToURL = true;
    > >
    > > /**
    > > * URL to connect to remote database (or MS Access local file)
    > > * e.g. jdbc:eek:racle:thin:mad:localhost:1521:prod1
    > > */
    > > private String url = "jdbc:eek:dbc:DRIVER={Microsoft Access Driver
    > > (*.mdb)};DBQ=osrmt.mdb";
    > >
    > > /**
    > > * Driver implementation - class name - must be in the classpath!
    > > */
    > > private String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
    > >
    > > /**
    > > * Database connections of the same environment name
    > > * will access URLs in this order
    > > */
    > > private int accessSequence = 0;
    > >
    > > private String username = "admin";
    > >
    > > private String password = "";
    > >
    > > public int getAccessSequence() {
    > > return accessSequence;
    > > }
    > >
    > > public void setAccessSequence(int accessSequence) {
    > > this.accessSequence = accessSequence;
    > > }
    > >
    > >
    > >
    > > public boolean isConnectToURL() {
    > > return connectToURL;
    > > }
    > >
    > > public void setConnectToURL(boolean connectToURL) {
    > > this.connectToURL = connectToURL;
    > > }
    > >
    > > public String getDriverClass() {
    > > return driverClass;
    > > }
    > >
    > > public void setDriverClass(String driverClass) {
    > > this.driverClass = driverClass;
    > > }
    > >
    > >
    > > public String getPassword() {
    > > return password;
    > > }
    > >
    > > public void setPassword(String password) {
    > > this.password = password;
    > > }
    > >
    > > public String getUrl() {
    > > return url;
    > > }
    > >
    > > public void setUrl(String url) {
    > > this.url = url;
    > > }
    > >
    > > public String getUsername() {
    > > return username;
    > > }
    > >
    > > public void setUsername(String username) {
    > > this.username = username;
    > > }
    > >
    > >
    > >
    > > }
    > >
     
    , Jun 22, 2006
    #3
  4. wrote:
    > ooooohhhhhh mannnn
    >
    > I've been banging my head against the wall for 3 hours - and all the
    > time it doesnt write it because my private variables are initialized to
    > that value????
    >
    > Well I say that is absurd ... so I persist the object to XML, later
    > change the default value of my *private* variables - and later
    > XML-->Object = wrong values
    >
    > icky


    Design trade-off. The API says it uses a "redundancy elimination
    algorithm," which is summarized in 2 sentences at

    http://java.sun.com/products/jfc/tsc/articles/persistence4/#intro

    The goal is to reduce how much is read and written.

    There are lots of ways changing your class could break things later on
    when decoding, so they may have felt it was a lost cause trying to
    prevent that.
     
    Chris Riesbeck, Jun 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cory Musselman

    Help with errors from XMLEncoder

    Cory Musselman, Nov 3, 2003, in forum: Java
    Replies:
    1
    Views:
    489
    Michael Borgwardt
    Nov 3, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,384
    Kaidi
    Jan 3, 2004
  3. Kaidi
    Replies:
    7
    Views:
    4,411
    Robert Olofsson
    Jan 6, 2004
  4. JMC
    Replies:
    1
    Views:
    779
    Roedy Green
    Apr 7, 2004
  5. Paul Kaiser
    Replies:
    1
    Views:
    593
    Chris Riesbeck
    May 24, 2004
Loading...

Share This Page