web游览器中的javascript冰没有一个通用的xml解析器用,xhr对象可以帮助我们解析xml响应!
客户端代码如下:
<html> <head> <title>Hello Ajax version 6</title> <style type='text/css'> * { font-family: Tahoma,Arial,sans-serif; } #helloTitle{ color: #48f; } .sidebar{ background-color: #adf; color: navy; border: solid blue 1px; width: 180px; height: 200px; padding: 2px; margin: 3px; float: left; } </style> <script type='text/javascript' src='prototype.js'></script> <script type='text/javascript'> window.onload=function(){ $('helloBtn').onclick=function(){ var name=$('helloTxt').value; new Ajax.Request( "hello6.jsp?name="+encodeURI(name),{ method:"get",onComplete:function(xhr){ var responseDoc=xhr.responseXML; //读取xml响应 从以前的responseText换的 update(responseDoc); } } ); } } function update(doc){//读取xml中的每一个元素,然后把数据转换为html语言呈现出来 var personNode=doc.getElementsByTagName('person')[0]; //获得第一个person 节点 var initial=personNode.getAttribute('initial');//利用getAttribute()函数通过名称获取属性的值 var nameNode=personNode.getElementsByTagName('name')[0]; var name=nameNode.firstChild.data; var likesNode=personNode.getElementsByTagName('likes')[0]; var likesList=likesNode.getElementsByTagName('item');//很明显的是一个数组 var likes=[]; for (var i=0;i<likesList.length;i++){ var itemNode=likesList[i]; likes[i]=itemNode.firstChild.data; } var recipeNode=personNode.getElementsByTagName('recipe')[0]; var recipeNameNode=recipeNode.getElementsByTagName('name')[0]; var recipeName=recipeNameNode.firstChild.data; var recipeSuggestNode=recipeNode.getElementsByTagName('serving-suggestion')[0]; var recipeSuggest=recipeSuggestNode.firstChild.data; var ingredientsList=recipeNode.getElementsByTagName('ingredient'); var ingredients={}; for(var i=0;i<ingredientsList.length;i++){ var ingredientNode=ingredientsList[i]; var qty=ingredientNode.getAttribute("qty"); var iname=ingredientNode.firstChild.data; ingredients[iname]=qty; } <!--下边的jquery代码就是装配HTML的 语句--> $('helloTitle').innerHTML="<h1>Hello,<b><i>"+name+"</i></b></h1>"; var likesHTML='<h5>'+initial+' likes...</h5><hr/>'; for (var i=0;i<likes.length;i++){ likesHTML+=likes[i]+"<br/>"; } $('likesList').innerHTML=likesHTML; var recipeHTML='<h5>'+initial+'\'s favorite recipe is '+recipeName+'</h5>'; for (key in ingredients){ recipeHTML+=key+" : "+ingredients[key]+"<br/>"; } recipeHTML+='<br/><i>'+recipeSuggest+'</i>'; $('ingrList').innerHTML=recipeHTML; } </script> </head> <body> <div id='likesList' class='sidebar'> <h5>Likes</h5><hr/> </div> <div id='ingrList' class='sidebar'> <h5>Ingredients</h5><hr/> </div> <div> <div id='helloTitle'> <h1>Hello,stranger</h1> </div> <p>Please introduce yourself by entering your name in the Box below</p> <input type='text' size='24' id='helloTxt'></input><button id='helloBtn'>Submit</button> </div> </body> </html>
jsp代码如下:(很明显的就是一个用xml写的)
<%-- simple JSP to generate some questions - and answers--%> <jsp:directive.page contentType="text/xml"/> <% String name=request.getParameter("name"); %> <person initial="<%=name.substring(0,1).toUpperCase()%>"> <name><![CDATA[<%=name%>]]></name> <likes> <item>JavaScript</item> <item>Skiing</item> <item>Apple Pie</item> </likes> <recipe> <name>apple pie</name> <ingredient qty="3kg">apples</ingredient> <ingredient qty="1kg">sugar</ingredient> <ingredient qty="2.4kg">pastry</ingredient> <serving-suggestion><![CDATA[Best Eaten Outdoors! Mmm!]]></serving-suggestion> </recipe> </person>
另外我们还要用到prototype.js的封装集;
然后我们就可以完成通信了!