Content is not allowed in prolog

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就可以

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值