标题:Ajax如何读取数据流中的xml文件?
只看楼主
wbl313
Rank: 1
等 级:新手上路
帖 子:69
专家分:0
注 册:2006-8-28
 问题点数:0 回复次数:0 
Ajax如何读取数据流中的xml文件?

这是我的源代码,请大家帮忙看看,我读到的怎么都是空值。是不是方法错了?
<script type="text/javascript">
var xmlHttp;

function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}
}

function createXML(){
var xml="<pets>";
var options=document.getElementById("petTypes").childNodes;
var option=null;
for(var i=0;i<options.length;i++){
option=options[i];
if(option.selected){
xml=xml+"<type>"+option.value+"<\/type>";
}
}
xml=xml+"<\/pets>"+"\n";
alert(xml);
return xml;
}

function sendPetTypes(){
createXMLHttpRequest();
var xml=createXML();
var url="/wms/wmtsppdmul.html?method=ajax&timeStamp="+new Date().getTime();
xmlHttp.open("POST",url,true);
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(xml);
}

function handleStateChange(){
alert(xmlHttp.readyState) ;
if(xmlHttp.readyState==4){
alert(xmlHttp.status) ;
if(xmlHttp.status==200){
alert("");
parseResults();
}
}
}

function parseResults(){alert(xmlHttp.responseText);
var responseDiv=document.getElementById("serverResponse");
if(responseDiv.hasChildNodes()){
responseDiv.removeChild(responseDiv.childNodes[0]);
}
var responseText=document.createTextNode(xmlHttp.responseText);
responseDiv.appendChild(responseText);
}

</script>

public void ajax(HttpServletRequest request,HttpServletResponse response) throws Exception {
// request.setCharacterEncoding("GB2312");
// response.setContentType("text/html;charset=GB2312");
String x=request.getParameter("");
String xml=readXMLFromRequestBody(request);
Document xmlDoc=null;
try{System.out.println("=============");
xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
}catch(ParserConfigurationException e){
System.out.println("ParserConfigurationException:"+e);
}catch(SAXException e){
System.out.println("SAXException:"+e);
}System.out.println("=====111111========");
NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");
System.out.println("=============");
String type=null;
String responseText="Selected Pets:";
System.out.println("=============");
for(int i=0;i<selectedPetTypes.getLength();i++){
type=selectedPetTypes.item(i).getFirstChild().getNodeValue();
responseText=responseText+""+type;
}
System.out.println("=====333333333========");
response.setContentType("text/xml");
response.getWriter().print(responseText);

}

private String readXMLFromRequestBody(HttpServletRequest request){
System.out.println("====2222222=========");
StringBuffer xml=new StringBuffer();
String line=null;
try{
BufferedReader reader=request.getReader();
System.out.println("wwwwwww======"+reader.readLine());
while((line=reader.readLine())!=null){
xml.append(line);
}
}catch(Exception e){
System.out.println("Error reading XML:"+e.toString());
}
System.out.println("efadsfdafd============="+xml.toString());
return xml.toString();
}

搜索更多相关主题的帖子: Ajax xml 数据 文件 
2007-03-09 09:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-123208-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.269365 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved