登 录
在使用JDom对xml进行处理的过程中遇到一个字符编码的问题:
public static Element getRootElement(String s) throws Exception{ InputStream in = null; try{ in = new StringBufferInputStream(new String(s.getBytes(), "ISO-8859-1")); DOMBuilder builder = new DOMBuilder(false); Element root = builder.build(in).getRootElement(); return root; }catch (Exception e){ throw e; }finally{ if (in != null){ in.close(); } } }
由于Stringbufferinputstream已经过时,据说存在编码问题,对处理包含中文内容的标签时,报错标签没有正确结束,于是只得更换方法:
public static Element getRootElement(String s) throws Exception { StringReader in = null; try { in = new StringReader(s); SAXBuilder builder = new SAXBuilder(false); org.jdom.Document document = builder.build(in); Element root = document.getRootElement(); return root; } catch (Exception e) { throw e; } finally { if (in != null) { in.close(); } } }
抱歉!评论已关闭.