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

  • SEARCH
  • Home
  • 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 8461989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:02:21+00:00 2026-06-10T14:02:21+00:00

today I faced with very interesting problem. When I try to rewrite xml file.

  • 0

today I faced with very interesting problem. When I try to rewrite xml file.

I have 3 ways to do this. And I want to know the best way and reason of problem.

I.

File file = new File(REAL_XML_PATH);
         try {
         FileWriter fileWriter = new FileWriter(file);
         XMLOutputter xmlOutput = new XMLOutputter();

     xmlOutput.output(document, System.out);
     xmlOutput.output(document, fileWriter);

     fileWriter.close();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }

In this case I have a big problem with my app. After writing in file in my own language I can’t read anything. Encoding file was changed on ANSI javax.servlet.ServletException: javax.servlet.jsp.JspException: Invalid argument looking up property: "document.rootElement.children[0].children"

II.

File file = new File(REAL_XML_PATH);
         XMLOutputter output=new XMLOutputter();
         try {
            output.output(document, new FileOutputStream(file));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In this case I haven’t problems. Encoding wasn’t change. No problem with reading and writing.

And this article http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html

And I want to know the best way and reason of problem.

  • 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-06-10T14:02:23+00:00Added an answer on June 10, 2026 at 2:02 pm

    Well, this looks like the problem:

    FileWriter fileWriter = new FileWriter(file); 
    

    That will always use the platform default encoding, which is rarely what you want. Suppose your default encoding is ISO-8859-1. If your document declares itself to be encoded in UTF-8, but you actually write everything in ISO-8859-1, then your file will be invalid if you have any non-ASCII characters – you’ll end up writing them out with the ISO-8859-1 single byte representation, which isn’t valid UTF-8.

    I would actually provide a stream to XMLOutputter rather than a Writer. That way there’s no room for conflict between the encoding declared by the file and the encoding used by the writer. So just change your code to:

    FileOutputStream fileOutput = new FileOutputStream(file);
    ...
    xmlOutput.output(document, fileOutput);
    

    … as I now see you’ve done in your second bit of code. So yes, this is the preferred approach. Here, the stream makes no assumptions about the encoding to use, because it’s just going to handle binary data. The XML writing code gets to decide what that binary data will be, and it can make sure that the character encoding it really uses matches the declaration at the start of the file.

    You should also clean up your exception handling – don’t just print a stack trace and continue on failure, and call close in a finally block instead of at the end of the try block. If you can’t genuinely handle an exception, either let it propagate up the stack directly (potentially adding throws clauses to your method) or catch it, log it and then rethrow either the exception or a more appropriate one wrapping the cause.

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

Sidebar

Related Questions

I faced an interesting problem today. I have 4 strings which I need to
Today I faced a strange problem in C#. I have an ASP.NET page where
Today I faced a strange problem. I have an application which is the same
I'm quite new to using NetBeans and today I faced a problem. I have
I was faced to a problem how to version a configuration file in XML
Today I have faced very strange behavior of sqlite3_finalize statement. I am using sqlite
i just faced a very strange problem today. My Application Structure is: /app -
Today i faced a little problem, i wanted to know the reason of leak.
Today I faced an interesting thing as FF File API and separate files by
today i hit a really annoing problem with wpf. i just want to align

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.