SAX parsing using JAXP (XMLReaderFactory, XMLReader, SAXParserFactory, and SAXParser)
1. We do this through XMLReaderFactory, XMLReader, as follows
1.adopt XMLReaderFactory Establish XMLReader object XMLReader reader = XMLReaderFactory.createXMLReader(); 2. Set Event Processor Object reader.setContentHandler(new MyDefaultHandler()); 3.Read to parse xml file FileReader fileReader =new FileReader(new File("src\\sax\\startelement\\web.xml")); 4.Specify parsed xml file reader.parse(new InputSource(fileReader));
Case: A more in-depth understanding of uri, localName, qName, and attribute s parameters through a case
1. First create the web.xml file to parse, as follows
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns:csdn="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <csdn:display-name></csdn:display-name> </web-app> <!-- uri - Namespace URI,If the element does not have any namespace URI,Or no namespace processing is being performed, which is an empty string. xml namespace-xmlns localName - Local name (without prefix) or empty string if namespace processing is not being performed. qName - A qualified name (with a prefix) or an empty string if the qualified name is not available. attributes - Attributes attached to elements.If there is no property, it will be empty Attributes Object. -->
2. Create internal class code for parsing test classes and event handling as follows
package sax.startelement; import java.io.File; import java.io.FileReader; import org.junit.Test; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class Demo3 { @Test public void test() throws Exception { // Creating an XMLReader object from an XMLReaderFactory XMLReader reader = XMLReaderFactory.createXMLReader(); // Set Event Processor Object reader.setContentHandler(new MyDefaultHandler()); // Read the xml file to parse FileReader fileReader = new FileReader(new File( "src\\sax\\startelement\\web.xml")); // Specify parsed xml file reader.parse(new InputSource(fileReader)); } // Custom parsing class that uses startElement to understand uri,localName,qName,Attributes class MyDefaultHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); System.out .println("--------------startElement Start execution--------------------------"); System.out.println("uri:::" + uri); System.out.println("localName:::" + localName); System.out.println("qName:::" + qName); for (int i = 0; i < attributes.getLength(); i++) { String value = attributes.getValue(i);// Get the value value of the property System.out.println(attributes.getQName(i) + "-----" + value); } System.out .println("------------------startElement completion of enforcement---------------------------"); } } }
3. The results of the program are as follows:
2. We did this through SAXParserFactory, SAXParser, XMLReader, as follows
1. Create a SAX parsing factory using SAXParserFactory
SAXParserFactory spf = SAXParserFactory.newInstance();
2. Obtain parser objects from SAX parsing factories
SAXParser sp = spf.newSAXParser();
3. Obtain an XML reader from the parser object
XMLReader xmlReader = sp.getXMLReader();
4. Set up event handlers for the reader
xmlReader.setContentHandler(new BookParserHandler());
5. Parse xml file
xmlReader.parse("book.xml");
Just using SAXParserFactory, SAXParser, they only need the following three steps to complete
1. Get the factory object for the sax parser
SAXParserFactory factory = SAXParserFactory.newInstance();
2. Create parser objects from factory object SAXParser
SAXParser saxParser = factory.newSAXParser();
3. Set parsed files and self-defined event handler objects by parsing the parse() method of saxParser
saxParser.parse(new File("src//sax//sida.xml"), new MyDefaultHandler());
Case: Resolve the text content in the Author element label
1. sida.xml file to parse
<?xml version="1.0" encoding="UTF-8"?> <! Four masterpieces of DOCTYPE [ <! ELEMENT's four masterpieces (Journey to the West, Dream of Red Chamber)> <! ATTLIST Journey to the West ID #IMPLIED> ]> <Four Great Books> <Journey to the West id="x001"> <Author>Wu Chengen</Author> </Journey to the West> <Dream of Red Chamber id="x002"> <Author>Cao Xueqin</Author> </Dream of Red Chamber> </Four Great Books>2. Parse implementation code for test class and event handler class
package sax; import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.junit.Test; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxTest { @Test public void test() throws Exception { // 1. Get the factory object for the sax parser SAXParserFactory factory = SAXParserFactory.newInstance(); // 2. Create parser objects from factory object SAXParser SAXParser saxParser = factory.newSAXParser(); // 3. Set parsed files and self-defined event handler objects by parsing the parse() method of saxParser saxParser.parse(new File("src//sax//sida.xml"), new MyDefaultHandler()); } // Self-defined event handlers class MyDefaultHandler extends DefaultHandler { // Identifier that resolves the start and end of a label boolean isOk = false; @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); // Set isOK to true when parsing author element begins if ("author".equals(qName)) { isOk = true; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); // Print the contents of an element when the parsed identifier is true if (isOk) { System.out.println(new String(ch, start, length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); // Set isOK to false when parsing the end of author element if ("author".equals(qName)) { isOk = false; } } } }
3. The results of the program are as follows: