日期:2014-05-16  浏览次数:20484 次

javascript解析xml文件(二)

下面这个方法是利用ajax技术去做的!同样经过测试,兼容IE和Firefox,和第一篇文章稍有不同!

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>numberDemo.xhtml</title>
		<meta http-equiv="keywords" content="enter,your,keywords,here" />
		<meta http-equiv="description" content="A short description of this page." />
		<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
		
		<!--<link rel="stylesheet" type="text/css" href="styles.css">-->
	</head>
	<script type="text/javascript" src="./scripts/prototype_1.6.js"></script>
	<script type="text/javascript" src="./scripts/OpenLayers_2.6_zy.js"></script>

</head>
	<script type="text/javascript">
		
		function init(){
			var city = $("number");
			//alert(city);
			
			var url="http://localhost:8080/ajax/my/numberServlet";
			var options={method:"get"};
			
              // 创建Ajax.Request对象,发起一个Ajax请求

              var myAjax = new Ajax.Request(
                  url, // 请求的URL
                  {
                      method: 'get', // 使用GET方式发送HTTP请求
                      onComplete: showResponse // 指定请求成功完成时需要执行的方法
                  }
              );
		}

		function showResponse(response){
			var doc = response.responseXML;
			
			var cs = doc.getElementsByTagName("number");		
			alert(cs.length)	
			for(var i=0;i<cs.length;i++){
				var a = cs[i];
				var b = a.childNodes;
				var c=b[0];
				var oOption = document.createElement("OPTION");
				document.getElementById("number").options.add(oOption);
				oOption.innerHTML = c.nodeValue;
				oOption.value = i;
			}
			
			
		}

	</script>
	<body onload="init()">
		<select id="number">
			
		</select>
	</body>
</html>

?

package com.fys.js.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class NumberServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

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

		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		
		//开始利用Dom4j生成xml文件
		Document doc = DocumentFactory.getInstance().createDocument();
		doc.setXMLEncoding("UTF-8");
		Element root = doc.addElement("numbers");
		for(int i = 1 ; i <= 20 ; i++){
			root.addElement("number").addText(i+"");
		}
		//文件写出去,但是没有写到磁盘,只是放在内存
		XMLWriter writer = new XMLWriter(out,OutputFormat.createPrettyPrint());
		writer.write(doc);
		
		out.flush();
		out.close();
	}

}

?使用到了dom4j和一些外部的js文件!具体自己下载来看!