Serializing an object not working properly - funky chars.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Serializing an object not working properly - funky chars.

  1. #1
    Join Date
    Jan 2009
    Posts
    8

    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. How should properly save this as an XML?
    And this object is Serializable.

    Thanks,

  2. #2
    Join Date
    Feb 2008
    Posts
    966

    Re: Serializing an object not working properly - funky chars.

    I had the same problem a while back and I had to use the following:

    Code:
        public void writeXML(String xml) throws Exception {
            OutputStream os = new BufferedOutputStream(new FileOutputStream("file.xml"));
            String format = "UTF-8";
            OutputStreamWriter osw = new OutputStreamWriter(os,format);
            osw.write(xml);
            osw.flush();
            os.close();
            osw.close();
        }
    You have to use a writer that you can set to an XML standard for it to format correctly and be recognized as XML. Just being able to "see" the tags and valid XML doesn't mean that the file is actually formatted correctly.

  3. #3
    Join Date
    Jan 2009
    Posts
    8

    Re: Serializing an object not working properly - funky chars.

    You already have an xml in string format. What I am looking for is to save complex java object as an xml to the database. Thanks,

  4. #4
    Join Date
    Jan 2009
    Posts
    8

    Re: Serializing an object not working properly - funky chars.

    It is working once i change it to XMLEnocder:

    ByteArrayOutputStream fos = new ByteArrayOutputStream();

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

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center