DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  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" ?>

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links