SAX parsing XML case Demo(2)

Posted by Crusader on Fri, 03 Apr 2020 09:06:33 +0200

I. Introduction

Create a new class XmlParseHandler.java, which needs to inherit DefaultHandler or implement ContentHandler interface. Here we inherit DefaultHandler (implement ContentHandler interface), which is the core of SAX parsing. We need to rewrite the following methods we care about.

  • startDocument(): called at the beginning of document parsing, this method will only be called once
  • startElement(String uri, String localName, String qName, 
    Attributes attributes: called at the beginning of label (node) parsing

    1. uri: namespace of xml document
    2. localName: the name of the label
    3. qName: the name of a label with a namespace
    4. attributes: attribute set of the tag
  • Characters (character [] ch, int start, int length): called when parsing the content of a label

    1. ch: byte array of textnode currently read
    2. start: the position where the byte starts. If it is 0, then read all
    3. Length: the length of the current TextNode
  • endElement(String uri, String localName, String qName): tags (nodes) are called after parsing.
  • endDocument(): when the document is parsed, it will be called once.

2, xml file

<?xml version = "1.0" encoding = "utf-8"?>
<books>
	<book id="1">
		<name>Song of ice and fire</name>
		<author>Zhang San</author>
		<pice>99</pice>
	</book>
	<book id="2">
		<name>Gourd doll</name>
		<pice>99</pice>
		<year>1993</year>
	</book>
</books>

3, XmlParseHandler

package com.da.xml;

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

public class SaxparserHedler extends DefaultHandler {
	int num = 0;
	
	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		
		if ("book".equals(qName)) {
			num++;
			//Name and number of known book Elements
			System.out.println("Attribute values:"+attributes.getValue("id"));
		
			//Name and number of unknown book Elements
			for (int i = 0; i < attributes.getLength(); i++) {
				System.out.println("The first"+(i+1)+"Attribute name:"+attributes.getQName(i)+"And attribute values"+attributes.getValue(i));
			}
		}else if(!"book".equals(qName) && !"books".equals(qName)) {
			System.out.print("Node name"+qName);
		}
		
	}
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		super.endElement(uri, localName, qName);
		if ("book".equals(qName)) {
			System.out.println("==========The first"+num+"book==========");
		}
	}
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		System.out.println("SAX Parsing begins");
	}
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
		System.out.println("SAX End of analysis");
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		super.characters(ch, start, length);
		String value = new String(ch, start, length);
		if (!value.trim().equals("")) {
			System.out.println(":"+value);
		}
	}
}

4, SAX parsing XML case Demo

package com.da.xml;

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


public class SaxTest {
	public static void main(String[] args) {
		//Get SAXParserFactory instance
		SAXParserFactory parserFactory = SAXParserFactory.newInstance();
		try {
			//Get SAXParser instance through SAXParserFactory
			SAXParser parser = parserFactory.newSAXParser();
			SaxparserHedler saxproHedler = new SaxparserHedler();
			parser.parse("books.xml", saxproHedler);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Five, expand

Using SAX parsing to store the content and structure of XML into JAVA objects

Topics: xml Attribute Java encoding