📜 ⬆️ ⬇️

Java. Simple SAX parser

In many tasks it becomes necessary to use all sorts of xml files for various purposes. I will not try to embrace the immensity, but I will tell you in my own experience why I need all this.


Java is probably my favorite programming language. In addition, this love is strengthened by the fact that any task is possible and there is no need to invent a bicycle.
So, it took me to create such a client-server connection working with a database that would allow the client to remotely make entries in the server's database. It must be validating input data, etc. etc., but it's not about that.
As a principle of work, I, without hesitation, chose the transfer of information in the form of an xml file. Type of the following:

<? xml version ="1.0" encoding ="UTF-8" standalone ="no" ? >
< doc >
< id > 3 </ id >
< fam > </ fam >
< name > </ name >
< otc > </ otc >
< dateb > 10-03-2005 </ dateb >
< datep > 10-03-2005 </ datep >
< datev > 10-03-2005 </ datev >
< datebegin > 09-06-2009 </ datebegin >
< dateend > 10-03-2005 </ dateend >
< vdolid > 1 </ vdolid >
< specid > 1 </ specid >
< klavid > 1 </ klavid >
< stav > 2.0 </ stav >
< progid > 1 </ progid >
</ doc >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" standalone ="no" ? >
< doc >
< id > 3 </ id >
< fam > </ fam >
< name > </ name >
< otc > </ otc >
< dateb > 10-03-2005 </ dateb >
< datep > 10-03-2005 </ datep >
< datev > 10-03-2005 </ datev >
< datebegin > 09-06-2009 </ datebegin >
< dateend > 10-03-2005 </ dateend >
< vdolid > 1 </ vdolid >
< specid > 1 </ specid >
< klavid > 1 </ klavid >
< stav > 2.0 </ stav >
< progid > 1 </ progid >
</ doc >


* This source code was highlighted with Source Code Highlighter .
<? xml version ="1.0" encoding ="UTF-8" standalone ="no" ? >
< doc >
< id > 3 </ id >
< fam > </ fam >
< name > </ name >
< otc > </ otc >
< dateb > 10-03-2005 </ dateb >
< datep > 10-03-2005 </ datep >
< datev > 10-03-2005 </ datev >
< datebegin > 09-06-2009 </ datebegin >
< dateend > 10-03-2005 </ dateend >
< vdolid > 1 </ vdolid >
< specid > 1 </ specid >
< klavid > 1 </ klavid >
< stav > 2.0 </ stav >
< progid > 1 </ progid >
</ doc >


* This source code was highlighted with Source Code Highlighter .


')
To make it easier to read further, I can only say that this is information about the medical institutions. Last name, first name, patronymic, unique id, and so on. In general a series of data. Further, this file safely got to the server side, and then began to parse the file.
Of the two options for parsing (SAX vs DOM), I chose SAX due to the fact that it works more efficiently, and it was the first that I got into my hands :)
So. As you know, to successfully work with the parser, we need to override the DefaultHandler methods we need. To begin with we will connect necessary packets.

import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;


* This source code was highlighted with Source Code Highlighter .
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;


* This source code was highlighted with Source Code Highlighter .
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;


* This source code was highlighted with Source Code Highlighter .


Now you can start writing our parser

public class SAXPars extends DefaultHandler{
...
}


* This source code was highlighted with Source Code Highlighter .
public class SAXPars extends DefaultHandler{
...
}


* This source code was highlighted with Source Code Highlighter .
public class SAXPars extends DefaultHandler{
...
}


* This source code was highlighted with Source Code Highlighter .


Let's start with the startDocument () method. He, as the name implies, responds to the event the beginning of the document. Here you can hang different actions for allocating memory, for example, or for resetting values, but our example is quite simple, so we simply mark the beginning of the work with the appropriate message:
@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .


Further. The parser follows the document, encounters an element of its structure. The startElement () method starts working. And in fact it looks like this: startElement (String namespaceURI, String localName, String qName, Attributes atts). Here namespaceURI is the namespace, localName is the local name of the element, qName is the combination of the local name with the namespace (separated by a colon) and atts are the attributes of this element. In our case, everything is simple. It is enough to use qName'om and throw it into some service line thisElement. Thus, we mark in which element at the moment we are.
@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}


* This source code was highlighted with Source Code Highlighter .


Further, having met an element we reach its value. This includes the characters () method. It looks like: characters (char [] ch, int start, int length). Well, everything is clear. ch is an array containing its own string-value itself inside this element. start and length are service numbers indicating the starting point in the string and the length.
@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

* This source code was highlighted with Source Code Highlighter .
@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

* This source code was highlighted with Source Code Highlighter .
@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

* This source code was highlighted with Source Code Highlighter .


Oh yes. I almost forgot. As an object, where I will dump the parsed data is an object of type Doctors. This class is defined and has all the necessary setters-getters.
Further, the element obviously ends and the next one follows. EndElement () is responsible for the ending. It signals that the item has ended and something can be done at this time. So do. Clear the Element.
@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}


* This source code was highlighted with Source Code Highlighter .


Having thus passed the entire document, we will come to the end of the file. EndDocument () will work. In it we can free up memory, make some kind of diagnostic print, etc. In our case, just write about that the parsing ends.
@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .
@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}


* This source code was highlighted with Source Code Highlighter .


Thus we received a class for parsing xml of our format. Here is the full text:
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class SAXPars extends DefaultHandler{

Doctors doc = new Doctors();
String thisElement = "" ;

public Doctors getResult(){
return doc;
}

@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}

@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}

@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}

@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}
}


* This source code was highlighted with Source Code Highlighter .
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class SAXPars extends DefaultHandler{

Doctors doc = new Doctors();
String thisElement = "" ;

public Doctors getResult(){
return doc;
}

@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}

@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}

@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}

@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}
}


* This source code was highlighted with Source Code Highlighter .
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;

public class SAXPars extends DefaultHandler{

Doctors doc = new Doctors();
String thisElement = "" ;

public Doctors getResult(){
return doc;
}

@Override
public void startDocument() throws SAXException {
System. out .println( "Start parse XML..." );
}

@Override
public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
thisElement = qName;
}

@Override
public void endElement( String namespaceURI, String localName, String qName) throws SAXException {
thisElement = "" ;
}

@Override
public void characters( char [] ch, int start, int length) throws SAXException {
if (thisElement.equals( "id" )) {
doc.setId( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "fam" )) {
doc.setFam( new String (ch, start, length));
}
if (thisElement.equals( "name" )) {
doc.setName( new String (ch, start, length));
}
if (thisElement.equals( "otc" )) {
doc.setOtc( new String (ch, start, length));
}
if (thisElement.equals( "dateb" )) {
doc.setDateb( new String (ch, start, length));
}
if (thisElement.equals( "datep" )) {
doc.setDatep( new String (ch, start, length));
}
if (thisElement.equals( "datev" )) {
doc.setDatev( new String (ch, start, length));
}
if (thisElement.equals( "datebegin" )) {
doc.setDatebegin( new String (ch, start, length));
}
if (thisElement.equals( "dateend" )) {
doc.setDateend( new String (ch, start, length));
}
if (thisElement.equals( "vdolid" )) {
doc.setVdolid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "specid" )) {
doc.setSpecid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "klavid" )) {
doc.setKlavid( new Integer( new String (ch, start, length)));
}
if (thisElement.equals( "stav" )) {
doc.setStav( new Float( new String (ch, start, length)));
}
if (thisElement.equals( "progid" )) {
doc.setProgid( new Integer( new String (ch, start, length)));
}
}

@Override
public void endDocument() {
System. out .println( "Stop parse XML..." );
}
}


* This source code was highlighted with Source Code Highlighter .


I hope the topic has helped to easily present the essence of the work of the SAX parser.
Do not judge strictly the first article :) I hope it was at least useful to someone.

UPD : To run this parser, you can use this code:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SAXPars saxp = new SAXPars();

parser.parse( new File ( "..." ), saxp);


* This source code was highlighted with Source Code Highlighter .
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
SAXPars saxp = new SAXPars();

parser.parse( new File ( "..." ), saxp);


* This source code was highlighted with Source Code Highlighter .

Source: https://habr.com/ru/post/62757/


All Articles