简体   繁体   中英

xstream handles non-english character

I have the following code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;

import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.swing.JOptionPane;

/**
 *
 * @author yccheok
 */
public class Test {
    @SuppressWarnings("unchecked")
    public static <A> A fromXML(Class c, File file) {
        XStream xStream = new XStream();
        InputStream inputStream = null;

        try {
            inputStream = new java.io.FileInputStream(file);
            Object object = xStream.fromXML(inputStream);
            if (c.isInstance(object)) {
                return (A)object;
            }
        }
        catch (Exception exp) {
            exp.printStackTrace();
        }
        finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                    inputStream = null;
                }
                catch (java.io.IOException exp) {
                    exp.printStackTrace();
                    return null;
                }
            }
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    public static <A> A fromXML(Class c, String filePath) {
        return (A)fromXML(c, new File(filePath));
    }

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

        try {
            outputStream = new FileOutputStream(file);
            xStream.toXML(object, outputStream);
        }
        catch (Exception exp) {
            exp.printStackTrace();
            return false;
        }
        finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                    outputStream = null;
                }
                catch (java.io.IOException exp) {
                    exp.printStackTrace();
                    return false;
                }
            }
        }

        return true;
    }

    public static boolean toXML(Object object, String filePath) {
        return toXML(object, new File(filePath));
    }

    public static void main(String args[]) {
        String s = "\u6210\u4EA4\u91CF";
        // print ???
        System.out.println(s);
        // fine! show 成交量
        JOptionPane.showMessageDialog(null, s);
        toXML(s, "C:\\A.XML");
        String o = fromXML(String.class, "C:\\A.XML");
        // show ???
        JOptionPane.showMessageDialog(null, o);
    }
}

I run the following code through command prompt in Windows Vista.

1) May I know why System.out.println unable to print out Chinese Character in console?

2) I open up the xstream file. The saved value is

<string>???</string>

How can I make xstream save Chinese Character correctly?

Thanks.

According to the XStream FAQ , it generates output (1) with whatever your platform default encoding is, and (2) without an XML prologue. Which is a really bad combination.

The FAQ recomments using toXml(Writer) . If you use an OutputStreamWriter , you can specify the encoding during construction. Since XStream doesn't emit a prologue, I recommend using "UTF-8", as that's what the XML spec requires.

Alternatively, I suppose you could follow one of the other recommendations in the FAQ, and manually write an XML prologue into the stream with your default encoding. I don't recommend that.

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

If the default character encoding on the platform isn't capable of displaying Chinese, you need to override it in the console and when launching Java. To set Java's character encoding, set the file.encoding property on the command-line (it won't work if you call System.setProperty() in your program).

java -Dfile.encoding=Big5 ...

I don't know the command for setting the console encoding in Vista. In Windows XP, it was the chcp ("change code page") command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM