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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:39:30+00:00 2026-06-11T10:39:30+00:00

I have a problem writing a file to a xml. Here is how this

  • 0

I have a problem writing a file to a xml.
Here is how this element looks in xsd.

<xs:element name="File">
        <xs:complexType>
            <xs:simpleContent>
                <xs:extension base="xs:base64Binary">   
                </xs:extension>
            </xs:simpleContent>
        </xs:complexType>
    </xs:element>

Here is my method to write:

       private void writeFile (XMLStreamWriter2 sw, final InputStream is){
//is - inputstream from file
            try {
                OutputStream output = null;
                InputStream input = new Base64InputStream(is, true);
                int count;

                    int BUFFER_SIZE = 4000;
                    byte[] buffer = new byte[BUFFER_SIZE];
                    output = new FileOutputStream(new File("D:\\test.txt"));
                    while ((count = input.read(buffer)) != -1) {

                            output.write(buffer, 0, count);
                            sw.writeBinary(buffer, 0, count);

                    }
                    is.close();
                    input.close();
                    output.close();
            } catch (XMLStreamException ex) {
                Logger.getLogger(StaxFinal.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(StaxFinal.class.getName()).log(Level.SEVERE, null, ex);
            } 
        }

The content of test.txt is valid base-64 string, while content of File xml elements file is not (i’m checking with http://www.opinionatedgeek.com/dotnet/tools/base64decode/). why

EDIT
Trying to use this, but i get a lot of carriage-return symbols #xd;

private void writeFile (XMLStreamWriter sw, InputStream is){
   //is - FileInputStream
    Reader reader = new InputStreamReader(new Base64InputStream(is, true));
    char[] buf = new char[4096];
    int n;

        while( (n = reader.read(buf)) >= 0 ) {
                sw.writeCharacters(buf, 0, n-3 );
        }
}

EDIT
Method:

writeRaw(char[] chars, int i, int i1)

works fine. Strange, but woodstox’s readElementAsBinary reads and decodes base64 for me. Why writeBinary doesn’t write valid base64?
Anyways, thank you skaffman! You are awesome!

  • 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-11T10:39:31+00:00Added an answer on June 11, 2026 at 10:39 am

    I assume you use the Base64InputStream from Apache Commons Codec. It also offers a constructor with four arguments, that can be used to turn off line breaks completely.
    Adjusting your method from the question you can write valid Base64 output like this:

    private void writeFile (XMLStreamWriter sw, InputStream is){
        //is - FileInputStream
        Reader reader = new InputStreamReader(new Base64InputStream(is, true, -1, null));
        char[] buf = new char[4096];
        int n;
        while( (n = reader.read(buf)) >= 0 ) {
            sw.writeCharacters(buf, 0, n);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this problem in my application: Step 1 - create an file (xml)
Basically I have an XML file that looks like this: <?xml version=1.0 encoding=UTF-8?> <data>
I have used this code in the past for writing and reading xml files.
I have a problem with encoding when writing XML files via QXmlStreamWriter in windows,
I have problem with my Java program. How I read xml -file which has
i am struck in problem of writing to an XML file. I know how
I'm having a problem writing Norwegian characters into an XML file using C#. I
I have a problem with writing a catch clause for an exception that is
I'm writing chat client/server app with c# and I have problem with threading. I
I have problem concerning python packages and testing. I'm writing an application using wx

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.