Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:42:21+00:00 2026-05-27T18:42:21+00:00

I have the following Problem with the XMLEncoder. I want to Serialize a Class

  • 0

I have the following Problem with the XMLEncoder. I want to Serialize a Class that looks like this:

public class MyClass{
    private Object myObject;
    private Object anotherObject;
    private static MyClass instance = new MyClass();

    [myObject and anotherObject are set in the class later...]

    public static MyClass getInstance(){
        return instance;
    }

    [getter and setter methods here] 
}

Now i want to Serialize the Object like this (inside MyClass):

 public void saveObject(){
        [try catch stuff not shown]
        FileOutputStream fos = new FileOutputStream(new File("object.xml"));
    XMLEncoder xmle = new XMLEncoder(fos);
    xmle.writeObject(instance);
    xmle.close();
}

But my object.xml does not contain the Values of myObject and anotherInstance and looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_24" class="java.beans.XMLDecoder">
 <object class="MyClass"/>
</java>

Whats going on there? Does the XMLEncoder detect a loop with the static Field of the Same Class and does not serialize it further? But i dont get any error…
Can i Mark that field as Not Serializable or something else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:42:21+00:00Added an answer on May 27, 2026 at 6:42 pm

    If you want to use XmlEncoder on any class ( classes that dont adhere to JavaBeans conventions ) , here is a helpful link.

    Advanced use of XmlEncoder

    EDIT: There is a little blurb in there that might be of help to you.

    XMLEncoder works by cloning the object graph and recording the steps that were necessary to create the clone. This way XMLEncoder has a “working copy” of the object graph that mimics the steps XMLDecoder would take to decode the file. By monitoring the state of this working copy, the encoder is able to omit operations that would set property values to their default value, producing concise documents with very little redundant information.

    So, in your example, if the values of myObject and anotherObject are set on the instance variable instead of being initialized as default, everything works as expected.

    public void saveObject(){
            [try catch stuff not shown]
            instance.setObject(new Object());
            FileOutputStream fos = new FileOutputStream(new File("object.xml"));
        XMLEncoder xmle = new XMLEncoder(fos);
        xmle.writeObject(instance);
        xmle.close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have following problem: class Request<T> { private T sw; public Request() { //How
I have the following problem. I have to implement a class that has an
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have following problem: I want to check (C#) if a thread has finished
I have following problem. I want to create a file system based session storage
I have following problem. I want to make some graphics in c# windows form.
I have the following problem: there is a class with a couple of string
I have following problem. I have typical master/detail UI scenario. I set main object
I have following problem that in a transformation (which is completely done in memory)
i have following problem: I have a class Foo which encapsulates a web-api. the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.