在web project里面如何读取XML文件
方法:
1.XML放在web Resource: webContent --->Web-INF----下面
2.HttpServlet下的代码:
package org.platform.servlet; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.Iterator; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.platform.jason.util.JasonUtil; import org.platform.token.AccessToken; import org.platform.xml.ServerConfiguration; /** * Servlet implementation class MenuLoader */ @WebServlet("/MenuLoader") public class MenuLoaderServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MenuLoaderServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { <span style="color:#990000;"> ClassLoader classLoader = this.getServletContext().getClassLoader(); java.net.URL url = classLoader .getResource("actionlist.xml"); ServerConfiguration.parseXml(url)</span>; } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } } public class ServerConfiguration { public static void parseXml(URL path) { ActionList action = new ActionList(); SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(path); } catch (DocumentException e) { e.printStackTrace(); } //拿到根节点后,想怎么做都可以了 Element root = doc.getRootElement(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。