zoukankan      html  css  js  c++  java
  • Invalid byte 1 of 1byte UTF8 sequence(XML读取异常)

    XML读取异常Invalid byte 1 of 1-byte UTF-8 sequence.

    用xstream进行JavaBean与xml之间的转换,无法设定字符集编码格式,会导致上述异常。

    解决方法:org.w3c.dom(java dom)解析xml文档,在此仅列出write方法:

     1 public static void write(String xmlFile, String encoding)
     2             throws ParserConfigurationException, FileNotFoundException,
     3             TransformerException, UnsupportedEncodingException {
     4         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     5         DocumentBuilder builder = factory.newDocumentBuilder();
     6         Document document = builder.newDocument();
     7         Element root = document.createElement("sememewords");
     8         document.appendChild(root);
     9 
    10         Element e = document.createElement("sememeword");
    11         e.setTextContent("java dom Test");
    12         root.appendChild(e);
    13 
    14         TransformerFactory tf = TransformerFactory.newInstance();
    15         Transformer transformer = tf.newTransformer();
    16         DOMSource source = new DOMSource(document);
    17         transformer.setOutputProperty(OutputKeys.ENCODING, "utf8");
    18         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    19         PrintWriter pw = new PrintWriter(
    20                 new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
    21                         xmlFile), encoding)));
    22         StreamResult result = new StreamResult(pw);
    23         transformer.transform(source, result);
    24         pw.flush();
    25         pw.close();
    26     }
  • 相关阅读:
    Java面向对象类与对象整理
    Java案例整理
    Java引用类型传递整理
    Java基础方法整理
    Java流程控制语句和数组整理
    Java流程语句
    Java运算符和引用数据类型(Scanner、Random)
    Java概念、语法和变量基础整理
    Mysql连接查询、子查询、联合查询 整理
    Mysql数据约束 整理
  • 原文地址:https://www.cnblogs.com/MaNog/p/3816759.html
Copyright © 2011-2022 走看看