0
回答
xml节点与浏览器的问题
滴滴云服务器,限时包月0.9元,为开发者而生>>>   

我用ajax读取从服务器中返回的xml文档时,<></>这样子的标签对,在firefox与chrome中算两个,但是在IE中算一个。那以后写程序怎么办?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript">

var xmlhttp;

function changeCity()

{

alert("enter! changeCity!");

var cities = document.getElementById("cities");

var selected = document.getElementById("province").selectedIndex;

alert(selected+" = selected!");

createXMLHttp();

xmlhttp.onreadystatechange = processXMLHttpRequest;

xmlhttp.open("GET", "getCity?selected="+selected, true);

xmlhttp.send();

}

function createXMLHttp()

{

alert("enter createXMLHttp!");

if (window.XMLHttpRequest)

{

// code for IE7+, Firefox, Chrome, Opera, Safari 

  xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5 

 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

}

function processXMLHttpRequest()

{

if ( xmlhttp.readyState==4 && xmlhttp.status==200 )

{

var xmlRoot;

alert("enter! process! 4 && 200");

var XMLDoc = xmlhttp.responseXML;

xmlRoot = XMLDoc.documentElement;

var city = xmlRoot.getElementsByTagName("city");

alert(city.length + " = city.length");

alert(document.getElementById("cities").options.length + " = options.length");

while( document.getElementById("cities").options.length > 0 )

{

document.getElementById("cities").removeChild(document.getElementById("cities").childNodes[0]);

}

for (var i=0; i<city.length; i++)

{

try

{
//alert("enter loop!");
//alert("after loop, name: " + city[0].childNodes[0].nodeName);
//alert("after loop, name1: " + city[0].childNodes[1].nodeName);
//alert("after loop, name0: " + city[0].childNodes[3].nodeName);
var option = document.createElement("option");
option.text = city[0].childNodes[1].firstChild.data;
option.value = city[0].childNodes[3].firstChild.data;
//option.text = "ok";
//option.value = "1";

document.getElementById("cities").options.add(option);
}
catch(e)
{
alert("message is : " + e.message);
}
}
}
}
</script>
<title>index</title>

</head>

<body>
<table align="center">
<caption>请选择</caption>
<tr>

<td>

<select name="province" id="province" onchange="changeCity()">
<option value="0">--请选择--</option>
<option value="1">广东省</option>
<option value="2">北京省</option>
</select>
</td>

<td>
<select name="cities" id="cities">
<option value="">--请选择--</option>
</select>
</td>
</tr>
</table>
</body>
</html>
 

服务器端的servlet如下:

import java.io.IOException;

import java.io.*;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class getCity

 */

@WebServlet("/getCity")

public class getCity extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public getCity() {

        super();

        // TODO Auto-generated constructor stub

    }

 

/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.setContentType("text/xml");

String selected = request.getParameter("selected");

PrintWriter out = response.getWriter();

out.println("<response");

if ( selected.equals("0") )

{

out.println("<city>");

out.println("<cityname>请选择省份</cityname>");

out.println("<cityvalue>1</cityvalue>");

out.println("</city>");

}

else if ( selected.equals("1") )

{

out.println("<city>");

out.println("<cityname>北京</cityname>");

out.println("<cityvalue>1</cityvalue>");

out.println("</city>");

out.println("<city>");

out.println("<cityname>天安门</cityname>");

out.println("<cityvalue>2</cityvalue>");

out.println("</city>");

out.println("<city>");

out.println("<cityname>故宫/cityname>");

out.println("<cityvalue>3</cityvalue>");

out.println("</city>");

}

else if ( selected.equals("2") )

{

out.println("<city>");

out.println("<cityname>广州</cityname>");

out.println("<cityvalue>1</cityvalue>");

out.println("</city>");

out.println("<city>");

out.println("<cityname>茂名</cityname>");

out.println("<cityvalue>2</cityvalue>");

out.println("</city>");

out.println("<city>");

out.println("<cityname>泗水/cityname>");

out.println("<cityvalue>3</cityvalue>");

out.println("</city>");

}

out.println("</response>");

out.flush();

out.close();

}

 

/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request,response);

}
}

举报
dreamers
发帖于6年前 0回/313阅
顶部