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     }
  • 相关阅读:
    C++Builder中的异常传递
    lpc1343 usb isp not work in linux and mac
    玩玩Hiweed linux 2.0
    有关 stringWithString 和 initWithString
    Windows mobile 中获取内存使用情况
    玩玩xubuntu 8.10
    升级我的ipaq hx2110到Windows Mobile 6.0
    面试技巧 from IBM
    常用Sql语句
    c#的事件机制示例代码: 猫> 老鼠, 主人
  • 原文地址:https://www.cnblogs.com/MaNog/p/3816759.html
Copyright © 2011-2022 走看看