package com.highsoft.monitor.down;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
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.Element;
import org.dom4j.io.SAXReader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class test extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml;charset=utf-8");
PrintWriter out = response.getWriter();
String prefix = getServletContext().getRealPath ("/") ;
String good= getServletContext().getContextPath();
// 从Servlet参数读取log4j的配置文件
SAXReader reader = new SAXReader();
String path=getServletContext().getRealPath("/")+"WEB-INF\\classes\\test.xml" ; //log4j配置文件的路径
File file = new File(path);
if(file.exists()){
try{
Document document =reader.read(file); //读xml文件
Element root= document.getRootElement();//得到根节点
Element memberElm=root.element("color");// "member"是节点名
String text=memberElm.getText(); //取得节点的文字
String text2=root.elementText("name"); //这个是取得根节点下的name字节点的文字.
// 取得某节点下名为"color"的所有子节点进行遍历
List listRow=document.selectNodes("color");
//List nodes=rootElm.elements("member");
for(Iterator it=listRow.iterator();it.hasNext();){
Element elm=(Element)it.next();
System.out.print(elm);
}
//4.取得某节点下名为"member"的所有字节点并进行遍历.
// List nodes = root.elements("color");
//
// for (java.util.Iterator it = nodes.iterator(); it.hasNext();) {
// Element elm = (Element) it.next();
// // do something
// System.out.println(elm);
// }
//
//
//对某节点下所有子节点进行遍历
// for(Iterator it=root.elementIterator();it.hasNext();){
// Element element=(Element)it.next();
// //do something
// }
}catch(Exception e){
System.out.print(e.getMessage());
}
}
// String tt =getServletContext().getResource("/WEB-INF/classes/test.xml").toString();
System.out.println ( path) ;
out.println("<colors>");
out.println("<color id='1'>");
out.println("<name>burger</name>");
out.println("<price>3.95</price>");
out.println("</color> ");
out.println("<color id='2'>");
out.println("<name>di er ge</name>");
out.println("<price>3.95</price>");
out.println("</color> ");
out.println("<color id='3'>");
out.println("<name>san ge a @</name>");
out.println("<price>3.95</price>");
out.println("</color> ");
out.println(" </colors> ");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}