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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:43:34+00:00 2026-05-16T17:43:34+00:00

Previously, to read XML in UTF-8 encoding through xstream, I am using DomDriver as

  • 0

Previously, to read XML in UTF-8 encoding through xstream, I am using DomDriver as follow :

XStream xStream = new XStream(new DomDriver("UTF-8"));

However, later I realize this is VERY slow. I use the following way :

Optimize loading speed of xstream

This works fine at least.

However, later, I realize the same technique cannot be applied to write XML. I will get all ??? characters.

This is the last workable code using DomDriver during write

public static boolean toXML(Object object, File file) {
    XStream xStream = new XStream(new DomDriver("UTF-8"));
    OutputStream outputStream = null;

    try {
        outputStream = new FileOutputStream(file);
        xStream.toXML(object, outputStream);
    }
    catch (Exception exp) {
        log.error(null, exp);
        return false;
    }
    finally {
        if (false == close(outputStream)) {
            return false;
        }
        outputStream = null;
    }

    return true;
}

The above code works fine. In order to match with the read method which doesn’t use DomDriver, I change the code to

public static boolean toXML(Object object, File file) {
    XStream xStream = new XStream();
    OutputStream outputStream = null;
    Writer writer = null;

    try {
        outputStream = new FileOutputStream(file);
        writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
        xStream.toXML(object, outputStream);
    }
    catch (Exception exp) {
        log.error(null, exp);
        return false;
    }
    finally {
        if (false == close(writer)) {
            return false;
        }
        if (false == close(outputStream)) {
            return false;
        }
        writer = null;
        outputStream = null;
    }

    return true;
}

This time, all my Chinese characters changes to ???

May I know anything I had done wrong?

  • 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-16T17:43:35+00:00Added an answer on May 16, 2026 at 5:43 pm

    Look at this code:

    outputStream = new FileOutputStream(file);
    writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
    xStream.toXML(object, outputStream);
    

    You’re creating a writer which will use UTF-8 – but then completely ignoring it!

    Try this instead:

    xStream.toXML(object, writer);
    

    Also as a matter of style, I’d encourage you to consider the following:

    • Don’t compare results with Boolean constants; just use if (foo) or if (!foo) instead
    • Catching Exception is very rarely a good idea; catch specific exceptions instead
    • Returning a Boolean value to indicate success or failure isn’t idiomatic Java; generally if something fails, an exception is better
    • If the first close fails, you’re quitting the method before the second close call, which probably isn’t what you want. (In fact, closing the OutputStreamWriter` will close the stream anyway, but consider the principle of the thing.)
    • Setting local variables to null at the end of a method is unnecessary and clutters up your code
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read a TreeMap from an XML file using XStream (I previously
I have previously read Spolsky's article on character-encoding, as well as this from dive
I am using Samsung Galaxy S 4G (the new one). Previously, I tested this
Here is a simplified version of the XML returned. <?xml version=1.0 encoding=UTF-8 ?> <kml
This code is supposed to read from an existing xml file, and write the
Although I have read a few previously answered questions regarding a similar issue, I
I just read a blog which says ...methods that couldn’t be inlined previously because
Okay so I read this post http://facebook.stackoverflow.com/questions/10373897/deleting-a-previosly-posted-article-with-opengraph-or-check-if-said-article-has but I'm using the news.reads action type.
I've read through previous topics on closures on stackflow and other sources and one
those of you who read my previous questions may already know this: I'm currently

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.