Note that there are some explanatory texts on larger screens.

plurals
  1. POxstream - correct way to save XML in UTF-8
    primarykey
    data
    text
    <p>Previously, to <strong>read</strong> XML in UTF-8 encoding through xstream, I am using DomDriver as follow :</p> <pre><code>XStream xStream = new XStream(new DomDriver("UTF-8")); </code></pre> <p>However, later I realize this is VERY slow. I use the following way :</p> <p><a href="https://stackoverflow.com/questions/3623546/optimize-loading-speed-of-xstream">Optimize loading speed of xstream</a></p> <p>This works fine at least.</p> <p>However, later, I realize the same technique cannot be applied to write XML. I will get all ??? characters.</p> <p>This is the last workable code using DomDriver during <strong>write</strong></p> <pre><code>public static boolean toXML(Object object, File file) { XStream xStream = new XStream(new DomDriver("UTF-8")); OutputStream outputStream = null; try { outputStream = new FileOutputStream(file); xStream.toXML(object, outputStream); } catch (Exception exp) { log.error(null, exp); return false; } finally { if (false == close(outputStream)) { return false; } outputStream = null; } return true; } </code></pre> <p>The above code works fine. In order to match with the <strong>read</strong> method which doesn't use DomDriver, I change the code to</p> <pre><code>public static boolean toXML(Object object, File file) { XStream xStream = new XStream(); OutputStream outputStream = null; Writer writer = null; try { outputStream = new FileOutputStream(file); writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8")); xStream.toXML(object, outputStream); } catch (Exception exp) { log.error(null, exp); return false; } finally { if (false == close(writer)) { return false; } if (false == close(outputStream)) { return false; } writer = null; outputStream = null; } return true; } </code></pre> <p>This time, all my Chinese characters changes to ???</p> <p>May I know anything I had done wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload