How to create XML with UTF-16


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to create XML with UTF-16

  1. #1
    Join Date
    Sep 2004
    Posts
    2

    How to create XML with UTF-16

    Anyone has example to create a xml with UTF-16 encoding? Thanks!!!

  2. #2
    Join Date
    Oct 2004
    Posts
    11
    How about using the OutputStreamWriter class which allows you to specify encoding.

    E.g.

    Code:
    import java.io.*;
    
    public class Test {
        private static String outputFile = "c:/testOutput.txt";
        
        public static void main(String[] args) {
            
            try {
                File output = new File(outputFile);
                output.createNewFile();
                FileOutputStream fOut = new FileOutputStream(output);
                OutputStreamWriter writer = new OutputStreamWriter(fOut, "UTF16");
                
                String testMsg = "Testing 123";
                writer.write(testMsg);
                
                writer.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    This is a simple test harness that shows you how to generate UTF-16 output in a file. How you format the file is up to you. You could easily create the XML from this without much work.

    But don't forget the doctype:

    Code:
    <?xml version="1.0" encoding="UTF-16" ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center