java基礎 xml 使用dom4j解析 xml文件 servlet根據pattern 找到class
阿新 • • 發佈:2018-03-11
col demo gpo string java OS pattern mapping unit
package com.swift.kaoshi; import java.io.File; import java.util.List; import java.util.Scanner; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.junit.Test; /* <servlets> <servlet> <servlet-name>servlet1</servlet-name> <servlet-class>cn.itheima.servlets.MyServlet1</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet1</servlet-name> <url-pattern>/s1</url-pattern> </servlet-mapping> <servlet> <servlet-name>servlet2</servlet-name> <servlet-class>cn.itheima.servlets.MyServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet2</servlet-name> <url-pattern>/s2</url-pattern> </servlet-mapping> </servlets> 二、編寫程序,請按以下要求解析XML文件: 1. 接收用戶輸入一個字符串,例如:”/s1”,要求程序可以通過檢索<servlet-mapping>元素下的<url-pattern>元素進匹配,匹配後獲取同級的<servlet-name>元素的值; 通過這個值再檢索<servlet>元素下的<servlet-name>元素的值進行匹配,匹配成功後,獲取同級下的<url-pattern>的值,並打印<url-pattern>的值。<servlet-class> 例如:用戶輸入:/s1,控制臺打印:cn.itheima.servlets.MyServlet1 用戶輸入:/s2,控制臺打印:cn.itheima.servlets.MyServlet2*/ //dom4j SAX遍歷 public class Demo_Dom4j { public static void main(String[] args) throws Exception { // SAXReader reader = new SAXReader(); // Document document = reader.read(new File("web.xml")); // String servlet_name = null; // // System.out.println("請輸入一個路徑字符串:");// Scanner scan = new Scanner(System.in); // String str = scan.nextLine(); // // Element root = document.getRootElement(); // List<Element> mappings = root.elements("servlet-mapping"); // for (Element element : mappings) { // System.out.println(element); // List<Element> urls = element.elements("url-pattern");// for (Element url : urls) { // System.out.println(url); // String text=url.getText(); // System.out.println(text); // if(text.equals(str)) { // Element name=element.element("servlet-name"); // servlet_name=name.getText(); // System.out.println(servlet_name); // } // } // } // System.out.println("==========================================================================="); // //準備檢索servlet // List<Element> servlets=root.elements("servlet"); // for(Element servlet:servlets) { // System.out.println(servlet); // List<Element> servlet_names=servlet.elements("servlet-name"); // for(Element servlet_n:servlet_names) { // String servlet_name1=servlet_n.getText(); // System.out.println(servlet_name1); // if(servlet_name1.equals(servlet_name)) { // Element servlet_class=servlet.element("servlet-class"); // System.out.println("需要查找的類的名稱是: "+servlet_class.getText()); // } // } // } } @Test public void show() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File("web.xml")); String servlet_name = null; System.out.println("請輸入一個路徑字符串:"); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); Element root = document.getRootElement(); List<Element> mappings = root.elements("servlet-mapping"); for (Element element : mappings) { System.out.println(element); List<Element> urls = element.elements("url-pattern"); for (Element url : urls) { System.out.println(url); String text=url.getText(); System.out.println(text); if(text.equals(str)) { Element name=element.element("servlet-name"); servlet_name=name.getText(); System.out.println(servlet_name); } } } System.out.println("==========================================================================="); //準備檢索servlet List<Element> servlets=root.elements("servlet"); for(Element servlet:servlets) { System.out.println(servlet); List<Element> servlet_names=servlet.elements("servlet-name"); for(Element servlet_n:servlet_names) { String servlet_name1=servlet_n.getText(); System.out.println(servlet_name1); if(servlet_name1.equals(servlet_name)) { Element servlet_class=servlet.element("servlet-class"); System.out.println("需要查找的類的名稱是: "+servlet_class.getText()); } } } } }
java基礎 xml 使用dom4j解析 xml文件 servlet根據pattern 找到class