`
terrencexu
  • 浏览: 122520 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

在Java中如何读取UTF-8格式的XML文件 - How to read UTF-8 XML file in Java

阅读更多

在使用JAXB unmarshal XML的时候碰到了一个异常: Invalid byte 1 of 1-byte UTF-8 sequence

public static Object unmarshal(InputStream xml, Class<?> clazz) {
    Object obj = null;

    try {
        JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());
        Unmarshaller u = jc.createUnmarshaller();

        obj = u.unmarshal(xml);
    } catch (JAXBException e) {
        throw new RuntimeException("Can't unmarshal this xml file, please check the error message: " + e.getMessage());
    }

    return obj;		
}

 问题出现在u.unmarshal(xml)这个地方,这句实际上调用的是SaxParser.parse()方法,这是一个encoding的问题,我们需要将输入流转换为UTF-8格式,然后再由SaxParser去解析该输入流, 解决方法如下:

public static Object unmarshal(InputStream xml, Class<?> clazz) {
    Object obj = null;
    
	try {
		JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());
		Unmarshaller u = jc.createUnmarshaller();
		
		Reader reader = new InputStreamReader(xml,"UTF-8");
		InputSource is = new InputSource(reader);
		is.setEncoding("UTF-8");
		
		obj = u.unmarshal(is);
	} catch (JAXBException e) {
		throw new RuntimeException("Can't unmarshal this xml file, please check the error message: " + e.getMessage());
	} catch (UnsupportedEncodingException e) {
		throw new RuntimeException("Doesn't support encoding: UTF-8, please check the error message: " + e.getMessage());
	}
	
	return obj;
}

下面将使用泛型进一步优化该方法:

public static <T> T unmarshal(InputStream xml, Class<T> clazz) {
	T obj = null;
	
	try {
		JAXBContext jc = JAXBContext.newInstance(clazz.getPackage().getName());
		Unmarshaller u = jc.createUnmarshaller();
		
		Reader reader = new InputStreamReader(xml,"UTF-8");
		Source source = new StreamSource(reader);
		JAXBElement<T> element = u.unmarshal(source, clazz);
		
		obj = element.getValue();
	} catch (JAXBException e) {
		throw new RuntimeException("Can't unmarshal this xml file, please check the error message: " + e.getMessage());
	} catch (UnsupportedEncodingException e) {
		throw new RuntimeException("Doesn't support encoding: UTF-8, please check the error message: " + e.getMessage());
	}
	
	return obj;
}

 

1
0
分享到:
评论

相关推荐

    how-to-read-a-local-xml-file-with-flex.pdf

    &lt;?xml version="1.0" encoding="utf-8"?&gt; initialize="myService.send()"&gt; &lt;![CDATA[ import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; [Bindable] private var myData:...

    C#学习的101个经典例子

    File - How-To File Notifications File - How-To File System Framework - Comparison of DataBinding in Web and Windows Forms Framework - Creating an Enterprise Services Component Framework - How-To ...

    drools-distribution-7.10.0.Final

    It contains information how to use it on your project (with Maven, ANT, ...). Sources ------- The source jars are in the sources directory. But to build from sources, pull the sources with git: ...

    Lerner -- Python Workout. 50 Essential Exercises -- 2020.pdf

    - **Objective:** Count the number of occurrences of each word in a text file. - **Key Concepts:** - Tokenizing text into words. - Counting occurrences using dictionaries. 21. **Longest Word per ...

    Java邮件开发Fundamentals of the JavaMail API

    Instructions on how to download and install the JavaMail API are contained in the course. In addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    javaapi源码文档-galaxy-excel:该API基于开放源代码的Microsoft文档JavaAPI-ApachePOI,您可以使用

    1,指定excel文件中需要读取的内容 2,对读取到的数据进行校验,提供默认的校验规则,支持自定义校验规则扩展 3,将读取到的excel数据转换为无父子关系的POJO数据集 4,支持一层父子关系的数据转换,即excel中数据...

    Debenu Quick PDF Library for Mac 10.12

    The samples folder contained in the same directory as this readme.txt file contains sample projects demonstrating how to use Debenu Quick PDF Library with objective-C. -------------------------------...

    xml.zip_xml

    在“example how to read xml data with php”的场景下,通常会涉及到以下步骤: 1. 加载XML文件:使用DOMDocument的`load()`或SimpleXML的`simplexml_load_file()`函数。 2. 遍历XML结构:通过DOM的`...

    apktool documentation

    &lt;?xml version="1.0" encoding="utf-8" standalone="no"?&gt; In addition to XMLs, resources such as 9 patch images, layouts, strings and much more are correctly decoded to source form. Decoding The ...

    php.ini-development

    The directory from the --with-config-file-path compile time option, or the ; Windows directory (C:\windows or C:\winnt) ; See the PHP docs for more specific information. ; ...

    spring-boot-reference.pdf

    12. What to Read Next III. Using Spring Boot 13. Build Systems 13.1. Dependency Management 13.2. Maven 13.2.1. Inheriting the Starter Parent 13.2.2. Using Spring Boot without the Parent POM 13.2.3. ...

    Apache Spark 2.x for Java Developers

    The book starts with an introduction to the Apache Spark 2.x ecosystem, followed by explaining how to install and configure Spark, and refreshes the Java concepts that will be useful to you when ...

    Servlet查询数据库案例--Query(java源码)

    // Read initialization parameters from the web.xml file ServletConfig config = getServletConfig(); String driverClassName = config.getInitParameter("driverClassName"); String url = config....

    外文翻译 stus MVC

    The file struts-config.xml instructs ActionServlet on how to use the extended classes. There are several advantages to this approach: • The entire logical flow of the application is in a ...

    XML sample code with dtd and xslstyles

    attachment is describing my one exam questiones' solution of how to program XML file, xsl and dtd styles, hereby I upload these sample code to help someone who want to learn the basic functionally ...

    Beginning PHP 5.3

    - **Cookies:** Discussion on cookies and how to set, read, and delete cookies in PHP. - **Query Strings:** Explanation of query strings and how they can be used to pass data between pages. - **...

    drupal 6.12

    For detailed information on how to configure a test server environment using a variety of operating systems and web servers, see "Local server setup" (http://drupal.org/node/157602) in the Drupal ...

    jira-manual.pdf

    Understanding how to create, assign, and manage issues is key to effective use of JIRA. Additionally, familiarize yourself with concepts like issue types, priorities, statuses, and workflows to ...

Global site tag (gtag.js) - Google Analytics