<>SAX analysis XML

<>1.XML(Extensible Markup Language Extensible markup language ),XML Is a document that describes data in text .

XML Document example :
<?xml version="1.0" encoding="UTF-8" ?> <people> <person personId="E01"> <name>
Tony Blair</name> <address>10 Downing Street,London,UK</address> <tel>(061)
98765</tel> <fax>(061) 98765</fax> <email>qijingjing01@126.com</email> </person>
<person personId="E02"> <name>Tony Blair</name> <address>10 Downing
Street,London,UK</address> <tel>(061) 98765</tel> <fax>(061) 98765</fax> <email>
qijingjing01@126.com</email> </person> </people>
<>2.XML Use of

(1) Act as display data ( with XML Act as a display layer )

(2) Store data ( Storage layer ) Function of

(3) with XML Description data , And pass it between the contact server and the rest of the system

In a way ,XML It is data encapsulation and information transmission technology .

<>3. What is? SAX?

SAX yes Simple Apl for XML Abbreviation of

<>
SAX Read and operate XML Faster data , Lighter method ,SAX Allows you to process a document while it is being read , This eliminates the need to wait for the entire document to be stored before taking action , It does not involve DOM Necessary overhead and concept jump ,SAX
api Is an event based API , Suitable for processing data streams , That is, the data is processed in turn with the flow of data .SAX
API It will notify you when certain events occur when it parses your document , When you respond to it , Data you don't save will be discarded

SAX API There are four main interfaces for handling events in ,ContenHandler,DTDHandler,EntiyResolver and ErrorHandler, because
DefaultHandler The four event processor interfaces are implemented . So we just need to inherit DefaultHandler Class .

<>4. Parsing steps

* first , We put XML File corresponding Person Write it out package com.lili.xmlAndJson; /** * @author:
QiJingJing * @create: 2021/7/17 */ public class Person { private String personId
; private String name; private String address; private String tel; private
String fax; private String email; public Person(){} public Person(String
personId, String name, String address, String tel, String fax, String email) {
this.personId = personId; this.name = name; this.address = address; this.tel =
tel; this.fax = fax; this.email = email; } public String getPersonId() { return
personId; } public void setPersonId(String personId) { this.personId = personId;
} public String getName() { return name; } public void setName(String name) {
this.name = name; } public String getAddress() { return address; } public void
setAddress(String address) { this.address = address; } public String getTel() {
return tel; } public void setTel(String tel) { this.tel = tel; } public String
getFax() { return fax; } public void setFax(String fax) { this.fax = fax; }
public String getEmail() { return email; } public void setEmail(String email) {
this.email = email; } @Override public String toString() { return "Person{" +
"personId='" + personId + '\'' + ", name='" + name + '\'' + ", address='" +
address+ '\'' + ", tel='" + tel + '\'' + ", fax='" + fax + '\'' + ", email='" +
email+ '\'' + '}'; } }
*
Create in test class SAX Parser factory object
// 1. Create a SAX Parser factory object SAXParserFactory saxParserFactory = SAXParserFactory.
newInstance();
*
Create a parser instance using a parser factory
//2. Create from factory object SAX Parser SAXParser saxParser = saxParserFactory.newSAXParser();
*
Write an event listener class , Need inheritance DefaultHandler class , Then rewrite five of them , I have marked the purpose of each method
package com.lili.xmlAndJson; import org.xml.sax.Attributes; import org.xml.sax.
SAXException; import org.xml.sax.helpers.DefaultHandler; import java.util.
ArrayList; import java.util.List; /** * @author: QiJingJing * @create:
2021/7/17 */ public class PersonHandler extends DefaultHandler { private List<
Person> personList; private Person p; // Object currently being resolved private String tag;
// Used to record the tag name currently being resolved public List<Person> getPersonList() { return personList; } //
Called when document parsing begins @Override public void startDocument() throws SAXException { super.
startDocument(); personList = new ArrayList<>(); System.out.println(" Start parsing document "); }
@Override public void endDocument() throws SAXException { super.endDocument();
System.out.println(" End of parsing document "); } // Used when parsing the start element /** * @param uri Namespace * @param
localName Label name without prefix * @param qName Prefixed tag name * @param attributes The collection of properties for the current tag *
@return: void * @author: QijingJing * @date: 2021/7/17 */ @Override public void
startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException { super.startElement(uri, localName, qName, attributes); if
("person".equals(qName)) { p = new Person(); String personId = attributes.
getValue("personId"); p.setPersonId(personId); } tag = qName; } // Used when parsing the end element
@Override public void endElement(String uri, String localName, String qName)
throws SAXException { super.endElement(uri, localName, qName); if ("person".
equals(qName)) { personList.add(p); } tag = null; } // Called when parsing text content @Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length); if (tag != null) { if ("name".equals(tag))
{ p.setName(new String(ch, start, length)); } else if ("address".equals(tag)) {
p.setAddress(new String(ch, start, length)); } else if ("tel".equals(tag)) { p.
setTel(new String(ch, start, length)); } else if ("fax".equals(tag)) { p.setFax(
new String(ch, start, length)); } else if ("email".equals(tag)) { p.setEmail(new
String(ch, start, length)); } } } }
*
Create a listener object in the test class , give XML Address of , Then start parsing
package com.lili.xmlAndJson; import org.junit.Test; import org.xml.sax.
SAXException; import javax.xml.parsers.ParserConfigurationException; import
javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import
java.io.IOException; import java.io.InputStream; import java.util.List; /** *
@author: QiJingJing * @create: 2021/7/17 */ public class XmlTest { @Test public
void test() throws ParserConfigurationException, SAXException, IOException { //
1. Create a SAX Parser factory object SAXParserFactory saxParserFactory = SAXParserFactory.
newInstance(); //2. Create from factory object SAX Parser SAXParser saxParser = saxParserFactory.
newSAXParser(); //3. Create a data processor ( We need to code ourselves ) PersonHandler personHandler = new
PersonHandler(); // 4. Start parsing InputStream is = Thread.currentThread().
getContextClassLoader().getResourceAsStream("com/lili/xmlAndJson/person.xml");
saxParser.parse(is,personHandler); List<Person> personList = personHandler.
getPersonList(); personList.forEach(System.out::println); } }
The output is as follows :
Start parsing document End of parsing document Person{personId='E01', name='Tony Blair', address='10 Downing
Street,London,UK', tel='(061) 98765', fax='(061) 98765',
email='qijingjing01@126.com'} Person{personId='E02', name='Tony Blair',
address='10 Downing Street,London,UK', tel='(061) 98765', fax='(061) 98765',
email='qijingjing01@126.com'} Process finished with exit code 0
<>SAX Analytical characteristics :

1. Event driven

2. Sequential read , Fast speed

3. Nodes cannot be read arbitrarily ( Poor flexibility )

4. Small memory consumption for parsing

5.SAX It is more suitable for use on equipment with higher performance requirements (Android Under development )

Technology
©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement