编程论坛's Archiver

wbl313 发表于 2007-3-9 09:28

Ajax如何读取数据流中的xml文件?

<P>这是我的源代码,请大家帮忙看看,我读到的怎么都是空值。是不是方法错了?<BR>&lt;script type="text/javascript"&gt; <BR>    var xmlHttp;<BR>            <BR>    function createXMLHttpRequest(){<BR>      if(window.ActiveXObject){<BR>        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");<BR>      }<BR>      else if(window.XMLHttpRequest){<BR>        xmlHttp=new XMLHttpRequest();<BR>      }<BR>    }<BR>    <BR>    function createXML(){<BR>      var xml="&lt;pets&gt;";<BR>      var options=document.getElementById("petTypes").childNodes;<BR>      var option=null;<BR>      for(var i=0;i&lt;options.length;i++){<BR>        option=options[i];<BR>        if(option.selected){<BR>          xml=xml+"&lt;type&gt;"+option.value+"&lt;\/type&gt;";<BR>        }<BR>      }<BR>      xml=xml+"&lt;\/pets&gt;"+"\n";<BR>      alert(xml);<BR>      return xml;<BR>    }<BR>    <BR>    function sendPetTypes(){<BR>      createXMLHttpRequest();<BR>      var xml=createXML();<BR>      var url="/wms/wmtsppdmul.html?method=ajax&amp;timeStamp="+new Date().getTime();<BR>      xmlHttp.open("POST",url,true);<BR>      xmlHttp.onreadystatechange=handleStateChange;<BR>      xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");<BR>      xmlHttp.send(xml);      <BR>    }<BR>        <BR>    function handleStateChange(){   <BR>    alert(xmlHttp.readyState) ;<BR>      if(xmlHttp.readyState==4){  <BR>      alert(xmlHttp.status) ;     <BR>        if(xmlHttp.status==200){<BR>        alert("");          <BR>          parseResults();<BR>      }<BR>    }<BR>  }    <BR>    <BR>    function parseResults(){alert(xmlHttp.responseText);<BR>      var responseDiv=document.getElementById("serverResponse");<BR>      if(responseDiv.hasChildNodes()){<BR>        responseDiv.removeChild(responseDiv.childNodes[0]);        <BR>      }      <BR>      var responseText=document.createTextNode(xmlHttp.responseText);<BR>      responseDiv.appendChild(responseText);          <BR>    }<BR>     <BR>&lt;/script&gt;</P>
<P>public void ajax(HttpServletRequest request,HttpServletResponse response) throws Exception {    <BR>//        request.setCharacterEncoding("GB2312");<BR>//        response.setContentType("text/html;charset=GB2312");<BR>        String x=request.getParameter("");<BR>        String xml=readXMLFromRequestBody(request);<BR>        Document xmlDoc=null;<BR>        try{System.out.println("=============");<BR>            xmlDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));<BR>        }catch(ParserConfigurationException e){<BR>            System.out.println("ParserConfigurationException:"+e);<BR>        }catch(SAXException e){<BR>            System.out.println("SAXException:"+e);<BR>        }System.out.println("=====111111========");<BR>        NodeList selectedPetTypes=xmlDoc.getElementsByTagName("type");<BR>        System.out.println("=============");<BR>        String type=null;<BR>        String responseText="Selected Pets:";<BR>        System.out.println("=============");<BR>        for(int i=0;i&lt;selectedPetTypes.getLength();i++){<BR>            type=selectedPetTypes.item(i).getFirstChild().getNodeValue();<BR>            responseText=responseText+""+type;<BR>        }<BR>        System.out.println("=====333333333========");<BR>        response.setContentType("text/xml");<BR>        response.getWriter().print(responseText);<BR>        <BR>    }<BR>    <BR>    private String readXMLFromRequestBody(HttpServletRequest request){<BR>        System.out.println("====2222222=========");<BR>        StringBuffer xml=new StringBuffer();<BR>        String line=null;<BR>        try{<BR>            BufferedReader reader=request.getReader();<BR>            System.out.println("wwwwwww======"+reader.readLine());<BR>            while((line=reader.readLine())!=null){                <BR>                xml.append(line);<BR>            }<BR>        }catch(Exception e){<BR>            System.out.println("Error reading XML:"+e.toString());            <BR>        }<BR>        System.out.println("efadsfdafd============="+xml.toString());<BR>        return xml.toString();<BR>    }</P>


页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.