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()); } } } } }