我拼写的是<?xml version='1.0' encoding='GB2312'?>,返回的却是<?xml version='1.0'?>,xml头中少了encoding='GB2312'。
我在Servlet中打印我拼的xml字符串时,发现xml拼写正确,在xml头中确有encoding='GB2312',但用MSXML2.DOMDocument装载我返回的xml字符串后,encoding='GB2312'这句就没有了,我百思不得其解,各位大哥你们知道怎么回事吗??
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//html代码
<html>
<head>
</head>
<body>
<input type=button value="读取服务器端返回数据XML" onclick="Read()"><br>
<script>
var xmldoc = new ActiveXObject("MSXML2.DOMDocument");
function Read(){
xmldoc.async="false";
xmldoc.load("http://195.2.199.196:8080/XMLHttp/parserxmlservlet");
var str=xmldoc.xml;
alert(str);
}
</script>
</body>
</html>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//Servlet代码
package javascriptxml;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ParserXMLServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/xml; charset=GB2312";
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
StringBuffer buffer = new StringBuffer(2048);
buffer.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
buffer.append("<xmldata>\n");
buffer.append("<band ID='ptitle'>\n");
buffer.append("<row>\n");
buffer.append("<ob>\n");
buffer.append("<on>");
buffer.append("Title");
buffer.append("</on>\n");
buffer.append("<ov>");
buffer.append("报表");
buffer.append("</ov>\n");
buffer.append("</ob>\n");
buffer.append("</row>\n");
buffer.append("</band>\n");
buffer.append("</xmldata>\n");
System.out.print(buffer);
out.print(buffer);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
}
4 个解决方案
#1
不知道哦,帮你顶吧
#2
private static final String CONTENT_TYPE = "text/xml";
这样试试
这样试试
#3
jFresH_MaN()(AbsolutelyFresh)(java欣人) 试过了不行
#4
真的很奇怪
你再调试一下把
比如把属性的双引号换成单引号
给其他元素多加几个属性,看还会不会丢了属性
呵呵,我只能这么帮你了
你再调试一下把
比如把属性的双引号换成单引号
给其他元素多加几个属性,看还会不会丢了属性
呵呵,我只能这么帮你了
#1
不知道哦,帮你顶吧
#2
private static final String CONTENT_TYPE = "text/xml";
这样试试
这样试试
#3
jFresH_MaN()(AbsolutelyFresh)(java欣人) 试过了不行
#4
真的很奇怪
你再调试一下把
比如把属性的双引号换成单引号
给其他元素多加几个属性,看还会不会丢了属性
呵呵,我只能这么帮你了
你再调试一下把
比如把属性的双引号换成单引号
给其他元素多加几个属性,看还会不会丢了属性
呵呵,我只能这么帮你了