ReadXmlUtil.javajava
package org.apache.list.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; public class ReadXmlUtil { /** * 匹配文件 */ private static final String FILE_TYPE = "xml"; /** * 用Sax讀取xml文件 * * @param fileName * 文件名 * @param nodeName * 標示符 * @return * 讀取的內容 * @throws Exception */ public static List<HashMap<String, String>> readXmlInfo(String fileName, String nodeName) throws Exception { // 文件爲空的狀況下 if (fileName == null || fileName.length() == 0) { return null; } // 不是XML文件的狀況下 if (!fileName.endsWith(FILE_TYPE)) { return null; } // 文件是否存在和文件夾判斷 File fileInfo = new File(fileName); if (!fileInfo.exists()) { return null; } if (fileInfo.isDirectory()) { return null; } // 得到文件輸入流 FileInputStream fs = new FileInputStream(new File(fileName)); try { SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser parser = spf.newSAXParser(); SaxHandler handler = new SaxHandler(nodeName); parser.parse(fs, handler); fs.close(); return handler.getList(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void main(String[] args) throws Exception { try { List<HashMap<String, String>> list = readXmlInfo("src/itcast.xml", "person"); for (HashMap<String, String> p : list) { System.out.println(p.toString()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
SaxHandler.javanode
package org.apache.list.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxHandler extends DefaultHandler { /** * 三級標籤 */ private HashMap<String, String> map = null; /** * 二級標籤 */ private List<HashMap<String, String>> list = null; /** * 正在解析的元素的標籤 */ private String currentTag = null; /** * 正在解析的元素的值 */ private String currentValue = null; private String nodeName = null; public List<HashMap<String, String>> getList() { return list; } public SaxHandler(String nodeName) { this.nodeName = nodeName; } /** * 當讀到一個開始標籤的時候,會觸發這個方法 */ public void startDocument() throws SAXException { list = new ArrayList<HashMap<String, String>>(); } /** * 當遇到文檔的開頭的時候,調用這個方法 */ public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { if (name.equals(nodeName)) { map = new HashMap<String, String>(); } if (attributes != null && map != null) { for (int i = 0; i < attributes.getLength(); i++) { map.put(attributes.getQName(i), attributes.getValue(i)); } } currentTag = name; } /** * 這個方法用來處理在XML文件中讀到的內容 */ public void characters(char[] ch, int start, int length) throws SAXException { if (currentTag != null && map != null) { currentValue = new String(ch, start, length); if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")) { map.put(currentTag, currentValue); } } currentTag = null; currentValue = null; } /** * 在遇到結束標籤的時候,調用這個方法 */ public void endElement(String uri, String localName, String name) throws SAXException { if (name.equals(nodeName)) { list.add(map); map = null; } super.endElement(uri, localName, name); } }
itcast.xmlapache
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>zhangsan</name> <age>30</age> </person> <person id="20"> <name>lisi</name> <age>25</age> </person> </persons>