---
title: "XML 中文解析出来是乱码问题"
author: "xiaoyangh"
date: "2011-10-23T08:58:11Z"
tags: []
categories: ["移动开发"]
slug: "xml"
draft: false
lastmod: "2011-10-22T20:53:31Z"
id: 1207858
url: "https://xiaoyangh.iteye.com/blog/1207858"
---

XML 里的中文解析出来是乱码。解决办法如下： 用 DOM 解析XML 时，载入XML 的步骤是： DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse( this.getInputSource() ); 其中builder 的 parse 方法可以接收多种类型的参数，可以是 File , InputStream , InputSource 翻了翻Doc，发现 InputSource 有 setEncoding() 方法，问题解决。。。。 把得到的 InputStream 包装成 InputSource ，然后设置 setEncoding( "GB2312" )。就可以看的 XML 里的中文了。