import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;
import mqq.sdet.dbt.protocol.DBTResp;
import mqq.sdet.xhttp.XResp;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* @author just
* @since XHttp 0.9
*
*/
public class XMLEndecoder {
public static final String XML_DECLARATION = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
public static final String DEFAULT_ENCODING = "UTF-8";
public static final String REQ = "REQ";
public static final String RESP= "RESP";
protected String encoding;
protected XStream xstream = new XStream(new DomDriver());
public XMLEndecoder(){
setEncoding(DEFAULT_ENCODING);
}
public XMLEndecoder(String en){
setEncoding(en);
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void encodeReq(Object req,OutputStream os) throws IOException{
encode(req,os);
}
public void encodeResp(XResp resp,OutputStream os) throws IOException{
encode(resp,os);
}
public void encode(Object object,OutputStream os) throws IOException{
if(object == null){
throw new IOException("Encode:Instance can not be null!");
}
Writer wt = null;
try {
wt = new OutputStreamWriter(os,encoding);
wt.write(XML_DECLARATION);
xstream.toXML(object, wt);
} catch (IOException e) {
throw e;
}finally{
if(wt != null){
wt.close();
}
}
}
public XResp decodeResp(InputStream is) throws IOException{
Reader reader = null;
XResp resp = null;
try {
reader = new InputStreamReader(is,encoding);
resp = (XResp)xstream.fromXML(reader);
} catch (IOException e) {
throw e;
}finally{
if(reader != null){
reader.close();
}
}
return resp;
}
// public XReq decodeReq(InputStream is) throws IOException{
// Reader reader = null;
// XReq req = null;
// try {
// reader = new InputStreamReader(is,encoding);
// req = (XReq)xstream.fromXML(reader);
// } catch (IOException e) {
// throw e;
// }finally{
// if(reader != null){
// reader.close();
// }
// }
// return req;
// }
@SuppressWarnings("unchecked")
public Map<String,String> decodeMap(InputStream is) throws IOException{
Reader reader = null;
Map<String,String> handleMap = null;
try {
reader = new InputStreamReader(is,encoding);
handleMap = (Map<String, String>) xstream.fromXML(reader);
} catch (IOException e) {
throw e;
}finally{
if(reader != null){
reader.close();
}
}
return handleMap;
}
//@SuppressWarnings("unchecked")
public Object decodeObject(InputStream is) throws IOException{
Reader reader = null;
Object object = null;
try {
reader = new InputStreamReader(is,encoding);
object = xstream.fromXML(reader);
} catch (IOException e) {
throw e;
}finally{
if(reader != null){
reader.close();
}
}
return object;
}
}
--------------------------------------------------------上面是解析xml为object方法------------------------------
-----------------------------调用如下------------------
public static void main(String[] args){
XMLEndecoder endecoder = new XMLEndecoder();
endecoder.setEncoding("UTF-8");
DBTResp resp;
try {
resp = (DBTResp) endecoder.decodeObject(new FileInputStream("C:\\DBTResp.xml"));
System.out.println(resp.getModuleName());
System.out.println(resp.taskResults.size());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果 DBTResp.xml 文件用记事本打开另存为utf-8就会报错。。。Content is not allowed in prolog
解决方法: 文件用记事本打开另存为ASCII就可以
分享到:
相关推荐
Download from your IP address is not allowed 百度网盘永久连接: QT下载: qt-opensource-linux-x64-5.8.0.run: 链接:https://pan.baidu.com/s/1sQ3tqPaWdDnmhBYAc_XR7g qt-opensource-linux-x64-5.13.1....
设置隐试打开PPT报错 Hiding the application window is not allowed
Host 主机名 is not allowed to connect the mysql server 原因:没有授权远程访问mysql 解决方法: cmd 中运行mysql -u root -p(如果报mysql 不是内部或外部命令,找到安装mysql对应的bin文件夹运行mysql.exe,...
document.write is not allowed in a script tag(解决方案).md
document.write is not allowed in a link tag(解决方案).md
document.write is not allowed in a style tag(解决方案).md
nginx: [emerg] “proxy_cache_path” directive is not allowed here in /etc/nginx/conf.d/default.conf:29 提示意思“proxy_cache_path指令不被允许”,在官网上查找了相关说明,也没有发现问题,最后看应用范围...
在使用MySQL数据库时,可能会遇到"Host 'localhost' is not allowed to connect to this MySQL server"的错误信息,这是由于MySQL的安全机制所致。MySQL的安全机制是基于主机名和用户名的组合来控制访问权限的。因此...
具体错误为“Content is not allowed in prolog”,这意味着在XML文档的声明之前存在非法的字符或内容,违反了XML规范。 #### 3. 错误的条件判断 代码示例中展示了一个错误的条件判断逻辑。在`initialize`方法中,...
mongod高级查询语句,Javascript查询和$where查询,正则表达式匹配
本文将详细介绍在使用curl访问特定域名时遇到405 Method Not Allowed错误的解决方法。首先,我们先来理解什么是405错误以及其背后可能的原因。 HTTP状态码405 Method Not Allowed表示客户端请求的HTTP方法不被...
NULL 博文链接:https://rayoo.iteye.com/blog/1973032
NR5G 网络拒绝码 - 5gmm_cause = 7 (0x7) (5GS Service not allowed) 本资源摘要信息将详细解释 NR5G 网络拒绝码 5gmm_cause = 7 (0x7) (5GS Service not allowed),并对相关知识点进行详细说明。 一、NR5G 网络...
CreateInstanceFromType is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. Called from ScriptableObject '...
Android WebView 报 Not allowed to load local resource错误的解决办法 博客地址:http://blog.csdn.net/yuzhiqiang_1993/article/details/76228541
标题 "Host 'localhost' Not Allowed To Connect To Server" 是一个常见的MySQL服务器错误,通常发生在尝试从本地主机连接到数据库服务时被拒绝的情况。这个错误意味着MySQL服务器的配置没有正确地允许来自localhost...
directories that will not be allowed to be processed by Diskeeper. Event Logging ------------- Diskeeper records information about its activity in two ways. First, general information about ...
import cycle not allowed(解决方案).md
message from server: “Host ‘****’ is not allowed to connect to this MySQL server 1:在登录mysql服务器 2:执行:GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’ WITH GRANT OPTION //赋予任何主机访问数据的...