71 SAX parsing XML Process

Analytical procedures and principles

  1. // create the corresponding entity classes based on xml tags
  2. // Create a SAX factory
  3. // Gets an instance of the SAX parser from factory
  4. // Create Handler subclass and new
  5. // override this subclass: corresponding to the tag writing contents of the entity class
  6. // Parse

SAX parsing xml files only go once, once parsed, resolution order from top to bottom, line by line resolution, resolution tag, and then parse the contents between tags (even blank, newline to be parsed), then resolution tag.

SAX can identify the beginning and closing tags, so we can easily do something in startElement () and endElement () method in.

characters () method is used to read the contents between two adjacent labels (not read the contents between the paired tag), even if the blank is also read out. For example: </ person> </ persons>, we can use the trim () will eliminate whitespace characters

Using a variable tag name to store the labels, may correspond to the current tag name in characters () method, for convenience in the tag and read the data.

Case presentation

The following analytical SAX xml used, the result is stored in the entity classes.

<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person>
		<Name> Jay </ name>
		<fans>3000w</fans>
	</person>
	<person>
		<Name> Tsai </ name>
		<fans>2000w</fans>
	</person>
</persons>

  

Code

Which show the method is not overridden method is custom methods.

Entity classes:

package _20191224_review;
/**
 * Person.xml in Person tags entity
*/
public class Person {
	private String name;
	private String fans;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getFans() {
		return fans;
	}
	public void setFans(String fans) {
		this.fans = fans;
	}
}

  

Resolution process:

package _20191224_review;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * SAX
 * Xml parsing
 * Code: 36
 */
public class TestXmlSAX {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		// Get SAX from the factory
		SAXParserFactory factory = SAXParserFactory.newInstance();
		// Get SAX parser from the
		SAXParser parser = factory.newSAXParser();
		// Create Handler subclass, and instantiate
		PersonHandler handler = new PersonHandler();
		Necessary methods in this class // rewrite
		// Parse
		parser.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("_20191224_review/person.xml"),handler);
		// Try to get the data
		handler.show();
	}
}

class PersonHandler extends DefaultHandler{
	private List<Person> persons;
	private String tag;
	private Person person;
	public PersonHandler() {
		persons = new ArrayList<>();
	}
	@Override
	public void startDocument() throws SAXException {
		System.out.println ( "began to parse the document");
	}
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		tag = qName;
		System.out.println ( "element start ->" + tag);
		if(tag.equals("person")) {
			person = new Person();
		}
	}
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		tag = qName;
		System.out.println ( "element end ->" + qName);
		if(tag.equals("person")) {
			persons.add(person);
		}
	}
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String content = new String(ch,start,length);
		if(content.trim().isEmpty()) {
			return;
		}
		System.out.println("tag "+tag+" 内容:"+content);
		if(tag.equals("name")) {
			person.setName(content);
		}
		if(tag.equals("fans")) {
			person.setFans(content);
		}
	}
	public void show() {
		Iterator it = persons.iterator();
		while(it.hasNext()) {
			Person p = (Person)it.next();
			System.out.println ( "name:" + p.getName () + "number of fans:" + p.getFans ());
		}
	}
}

  

 

running result

Guess you like

Origin www.cnblogs.com/Scorpicat/p/12090833.html