在使用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;
}
分享到:
相关推荐
<?xml version="1.0" encoding="utf-8"?> initialize="myService.send()"> <![CDATA[ import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; [Bindable] private var myData:...
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 ...
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: ...
- **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 ...
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...
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 ...
1,指定excel文件中需要读取的内容 2,对读取到的数据进行校验,提供默认的校验规则,支持自定义校验规则扩展 3,将读取到的excel数据转换为无父子关系的POJO数据集 4,支持一层父子关系的数据转换,即excel中数据...
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. -------------------------------...
在“example how to read xml data with php”的场景下,通常会涉及到以下步骤: 1. 加载XML文件:使用DOMDocument的`load()`或SimpleXML的`simplexml_load_file()`函数。 2. 遍历XML结构:通过DOM的`...
<?xml version="1.0" encoding="utf-8" standalone="no"?> In addition to XMLs, resources such as 9 patch images, layouts, strings and much more are correctly decoded to source form. Decoding The ...
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. ; ...
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. ...
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 ...
// Read initialization parameters from the web.xml file ServletConfig config = getServletConfig(); String driverClassName = config.getInitParameter("driverClassName"); String url = config....
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 ...
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 ...
- **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. - **...
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 ...
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 ...