Results 1 to 5 of 5
  1. #1
    getstarted is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default Serializing an object not working properly - funky chars.

    I have a java webservice.. I need to log the response before returning. I am trying to serialize as below:

    ByteArrayOutputStream fos = new ByteArrayOutputStream();
    ObjectOutputStream outStream = new ObjectOutputStream( fos );
    outStream.writeObject( response );
    outStream.flush();
    return fos.toString();

    But I see invalid characters all over. What is the process of saving it as proper xml text content? This object is Serializable and complex data type.

    Thanks,

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Serialization creates binary output, some of which may be legible, such as the contents of String. It is not intended to be human readable.

    You can use XMLEncoder on any object that follows the Java Beans conventions, but it will only save public properties (fields with public getXxx, isXxx, setXxx methods).

    If you want to save the response and read it back as as Java object, user serialization. If you want a human-readable archive, use XMLEncoder.

  3. #3
    getstarted is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    any example to convert from object to string(XML) using XMLEncoder?

  4. #4
    getstarted is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default It works fine when I changed to XMLEncoder

    ByteArrayOutputStream fos = new ByteArrayOutputStream();

    try {
    XMLEncoder encoder = new XMLEncoder(fos);
    encoder.writeObject(response);
    encoder.flush();
    return fos.toString();
    }

    Thanks,

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

Similar Threads

  1. event handler not working properly
    By H3rtaherta in forum Java 2D
    Replies: 3
    Last Post: 11-24-2008, 02:39 AM
  2. Object not serializing
    By MamboBanda in forum New To Java
    Replies: 1
    Last Post: 08-12-2008, 12:15 AM
  3. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 PM
  4. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 12:13 PM
  5. How do I omit an object when serializing?
    By Hasan in forum Advanced Java
    Replies: 1
    Last Post: 05-31-2007, 04:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •