SAX解析xml

    @SuppressWarnings("unchecked")
    public static void readCityXML(HttpServletRequest request) throws Exception {
        String path = request.getSession().getServletContext().getRealPath("/");
     //String path = Object.class.getResource("/").getPath();
     // File file
= new File(path + "WEB-INF/classes/config/xml/cities.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(file); Element root = doc.getRootElement(); Element foot = null; List<String> cities = new ArrayList<String>(); Iterator i = root.elementIterator(); while (i.hasNext()) { foot = (Element) i.next(); String name = foot.getText(); cities.add(name); } request.getSession().getServletContext().setAttribute("cities", cities); }
原文地址:https://www.cnblogs.com/sallet/p/3941609.html