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 56119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:29:04+00:00 2026-05-10T17:29:04+00:00

Got a class that serializes into xml with XMLEncoder nicely with all the variables

  • 0

Got a class that serializes into xml with XMLEncoder nicely with all the variables there. Except for the one that holds java.util.Locale. What could be the trick?

  • 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. 2026-05-10T17:29:05+00:00Added an answer on May 10, 2026 at 5:29 pm

    The problem is that java.util.Locale is not a bean. From the XMLEncoder doc:

    The XMLEncoder class is a complementary alternative to the ObjectOutputStream and can used to generate a textual representation of a JavaBean in the same way that the ObjectOutputStream can be used to create binary representation of Serializable objects.

    However, the API allows you to use PersistenceDelegates to serialize non-bean types:

    Sample bean:

    public class MyBean implements Serializable {      private static final long serialVersionUID = 1L;      private Locale locale;     private String foo;      public MyBean() {     }      public Locale getLocale() {         return locale;     }      public void setLocale(Locale locale) {         this.locale = locale;     }      public String getFoo() {         return foo;     }      public void setFoo(String foo) {         this.foo = foo;     }  } 

    Serializing a data graph that includes a Locale type:

    public class MyBeanTest {      public static void main(String[] args) throws Exception {         // quick and dirty test          MyBean c = new MyBean();         c.setLocale(Locale.CHINA);         c.setFoo('foo');          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();         XMLEncoder encoder = new XMLEncoder(outputStream);         encoder.setPersistenceDelegate(Locale.class, new PersistenceDelegate() {             protected Expression instantiate(Object oldInstance, Encoder out) {                 Locale l = (Locale) oldInstance;                 return new Expression(oldInstance, oldInstance.getClass(),                         'new', new Object[] { l.getLanguage(), l.getCountry(),                                 l.getVariant() });             }         });         encoder.writeObject(c);         encoder.flush();         encoder.close();          System.out.println(outputStream.toString('UTF-8'));          ByteArrayInputStream bain = new ByteArrayInputStream(outputStream                 .toByteArray());         XMLDecoder decoder = new XMLDecoder(bain);          c = (MyBean) decoder.readObject();          System.out.println('===================');         System.out.println(c.getLocale());         System.out.println(c.getFoo());     }  } 

    This is the section of code that describes how the object should be instantiated on deserialization – it sets the constructor arguments to three string values:

        new PersistenceDelegate() {         protected Expression instantiate(Object oldInstance, Encoder out) {             Locale l = (Locale) oldInstance;             return new Expression(oldInstance, oldInstance.getClass(),                     'new', new Object[] { l.getLanguage(), l.getCountry(),                             l.getVariant() });         }     } 

    Read Using XMLEncoder by Philip Milne for more info.

    All this aside, it might be smarter to store the locale information in textual form and use it to look up the appropriate Locale object whenever it is needed. That way you don’t need special case code when serializing your object and make it more portable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, in VSS you can manually associate different parts of… May 12, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer PyQt 4.6, using the modern API: The QVariant class is… May 12, 2026 at 3:56 pm
  • Editorial Team
    Editorial Team added an answer I am confused by java project "using log4j", since the… May 12, 2026 at 3:56 pm

Related Questions

I've got a class that I'm using as a settings class that is serialized
I'm attempting to deserialize a custom class via the XmlSerializer and having a few
I've got an interface that two classes implement at the moment. The data for
My question is the following. I have xml that is versioned by a namespace.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.