1.要解析的xmljava
<?xml version="1.0" encoding="UTF-8"?> <employees> <employee id="001"> <name>cici</name> <department>finace</department> <supervisor>lily</supervisor> </employee> <employee id="002"> <name>alex</name> <department>develope</department> <supervisor>lily</supervisor> </employee> </employees>
2.繼承DefaultHandler的子類EmployeeHandler.java,重寫方法ide
package sax; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; public class SaxXMLTest { public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{ readXMLBySaxParser(); readXMLByXMLReader(); } private static void readXMLBySaxParser() throws ParserConfigurationException, SAXException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); EmployeeHandler handler = new EmployeeHandler("employee"); parser.parse("src\\sax\\employees.xml", handler); List<Map<String, String>> employees = handler.getEmployees(); System.out.println(employees.toString()); } private static void readXMLByXMLReader() throws SAXException, SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException { XMLReader reader = XMLReaderFactory.createXMLReader(); //打開解析器驗證的功能 reader.setFeature("http://xml.org/sax/features/validation",true); //開啓明明空間特性 reader.setFeature("http://xml.org/sax/features/namespaces",true); EmployeeHandler handler = new EmployeeHandler("employee"); reader.setContentHandler(handler); reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\\sax\\employees.xml")))); } }
3.測試類 SaxXMLTest.java,用SAXParser和XMLReader兩種方式解析測試
package sax; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; public class SaxXMLTest { public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{ readXMLByHandler(); readXMLByXMLReader(); } private static void readXMLByHandler() throws ParserConfigurationException, SAXException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); EmployeeHandler handler = new EmployeeHandler("employee"); parser.parse("src\\sax\\employees.xml", handler); List<Map<String, String>> employees = handler.getEmployees(); System.out.println(employees.toString()); } private static void readXMLByXMLReader() throws SAXException, SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException { XMLReader reader = XMLReaderFactory.createXMLReader(); //打開解析器驗證的功能 reader.setFeature("http://xml.org/sax/features/validation",true); //開啓明明空間特性 reader.setFeature("http://xml.org/sax/features/namespaces",true); EmployeeHandler handler = new EmployeeHandler("employee"); reader.setContentHandler(handler); reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\\sax\\employees.xml")))); } }