jsp xml字符串decode后不能读取
sp xml字符串decode后不能读取
一个返回数据是xml,转换为字符串后在页面显示没有了xml节点。
我自己有一个ReadXmlString的类,将读取的字符串转换为map,以前都运行正常,能够正常输出(测试用的一个str):
String xml = "<xml><A>123</A><B>whl123</B><C>亮亮</C><D>1</D><E>1</E><F>165074</F><G>贫穷</G><H>1698.0</H><I>初级士官</I><J>湖南</J><K>常德</K><L>1</L></XML>";
out.println(readXml.read(xml)); //返回的map,将xml节点取出后加入到map中
正常输出:
{A=123, B=whl123, C=亮亮, D=1, E=1, F=165074, G=贫穷, H=1698.0, I=初级士官, J=湖南, K=常德, L=1}
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(new InputSource(new StringReader(protocolXML))); //parse是之前的方法名字,改为了read
Element root = doc.getDocumentElement();
NodeList books = root.getChildNodes();
if (books != null) {
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
map.put(book.getNodeName(),book.getFirstChild().getNodeValue());
System.out.println("节点=" + book.getNodeName() + "\ttext="
+ book.getFirstChild().getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
但是问题来了,我要连接的接口返回的xml字符串,怎么都不能转换为map,如果不encode,直接就显示的数据,decode后,也只是显示数据,而没有显示节点。我怀疑就是因为没有找到节点标识,所以不能转为map。
如果不encode显示的就是://jsp中out.print(data);
Faild 密码为空 0 0 0
encode后显示的就是://jsp中out.print(data)
%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22+%3F%3E%3Creturnsms%3E++++%3Creturnstatus%3EFaild%3C%2Freturnstatus%3E++++%3Cmessage%3E%E9%94%99%E8%AF%AF%E7%9A%84%E6%89%8B%E6%9C%BA%E5%8F%B7%EF%BF%BD%3F%3C%2Fmessage%3E++++%3Cremainpoint%3E0%3C%2Fremainpoint%3E++++%3CtaskID%3E%3C%2FtaskID%3E++++%3CsuccessCounts%3E0%3C%2FsuccessCounts%3E%3C%2Freturnsms%3E
(证明它确实有xml节点标识)
decode后显示:
Faild 密码为空 0 0 0
不过无论如何,始终readxml都不能转为map,ReadXmlString类没有读取到这个xml字符串的节点,是为什么呢?
(同时还有一个问题,decode后,怎么还是有部分字是乱码??)
this.status = new String(send.sendPost(this.url,prame).getBytes("gbk"),"utf-8"); //send是发送post请求
this.status = URLEncoder.encode(this.status,"utf-8");
this.status = URLDecoder.decode(this.status,"utf-8");
显示还是有乱码,大部分是正确的。