Courses Java - Playing a flow via XML SAX PDF Print E-mail
User Rating: / 0
PoorBest 
Friday, 18 July 2008

Introducing SAX
Simple API for XML or SAX is a general API for reading an XML. There are implementations of this API in all languages that you probably know (in any case exists for the implementation C + +, C #, Java, Pascal, Perl, PHP, ...) because XML was widely imposed today in the world of software for exchanging information. There are two major API to read the XML: DOM and SAX. SAX event is (we will discover details) while DOM transforms the tree XML tree target language. The major interest of DOM is the opportunity it offers to come and go at your convenience in the tree, its major drawback is the cumbersome nature of treatment. Indeed, as SAX events, his salary is over inflow.

I said that is SAX events, which means that there events in an XML file? Well, if one considers the incoming XML, it becomes easier to find events: an opening tag is an event, a closing tag another event ... So much for any say most of this API, hence its name Simple API for XML, once you know respond to these two events, you can already handle a XML to use an XML configuration file. It is surprisingly simple, and finally extremely powerful as we will découvrir.Quand you discovered this API, it is likely that you choose to replace all your files properties type pair key / value by an XML file and a rereading SAX . The advantage of XML configuration is its ability contextual, which is difficult to represent in a properties file. Indeed, in the form of name / value pairs, the notion contextual is generally poorly represented by key with a form of path (mon.chemin.vers.ma.propriete).

We look a little more detail, the API which is a bit more complicated than I have presented for the moment, especially because it reacts much more than the simple events of opening and closing tags . We find in particular those events for the opening and closing the xml document, events for the opening and closing of meta information.
Discover API

Content Handler
Let's begin how we can respond to these famous events generated by the SAX parser. To do this we done enough to implement the ContentHandler interface which is in the org.xml.sax package, then instantiate the parser giving what content manager will have to use it (ie ours) and the tour will be played virtually. Here is an overview of the central interface of your developments SAX: ContentHandler manager or content. The manager is indeed the one who does the real work of analysis while the other interfaces of the API made satellites work like managing errors, identify the "slider" in the flow during the analysis. These interfaces satellites are not to neglect but have a less central role in your development.

setDocumentLocator
A locator allows you to locate the "slider" for the treatment of flows for example allowing you to know the number of row and column being analysed. This is a very interesting feature at debug but it must at all costs avoid, as I say, you prohibit use for the treatment itself. In the API Helper sax, you provide a default implementation of all interfaces. If the implementation default ContentHandler is strictly unnecessary, the Locator should amply sufficient to 99% of developments, therefore I do not dwell on this point.

start Document
This method is called by the parser one and only once at the start of the analysis of your feed xml. It is called before all other methods of the interface, with the single exception, of course, the method setDocumentLocator. This event should help to initiate anything that has to be before the start of the trail of the document.

end Document
And its opposite, this method is called at the end of the trail flow after all other methods. It may be useful at this time to notify other objects that the work is finished.

processing Instruction
This event is raised for each instruction operation encountered. These instructions are those that you find out the tree xml itself such as instructions on dtd or simply the statement:
<? xml version = "1.0" encoding = "ISO-8859-1"?>

startPrefixMapping
This event is launched whenever a mapping in advance, which is a beacon in a namespace (name space), is met.

endPrefixMapping
The event obviously, it means the end of treatment in a namespace.

start Element
Starting an XML element ... Finally! And yes, you can indeed to start with SAX itself to understand this event and its treatment and its opposite to analyze a flow xml a very powerful and effective. We will therefore look a little more about this event.

start Element (String namespace Uri, String local Name, String raw Name, Attributes acts);
* Where namespace Uri is the string containing the URI full namespace tag or an empty string if the tag is not included in a namespace,
* Local Name is the name of the tag without the prefix if there were one,
* Raw Name is the name tag xml version 1.0 i.e. $ prefix: $ local name,
* Finally attributes is a list of attributes of the tag that will investigate a little further.

end Element
Unlike signature event much easier, since only the full name tag needs to be known. Indeed, the closure of the XML tag, no attribute is required.

characters
Anything that is in the tree but is not an integral part of a tag, triggering the lifting of this event. In general, this event is therefore lifted simply by the presence of text between the opening tag and the closing tag like this:

<maBalise> a little text </ maBalise>

The presence of "some text" provokes the lifting of the event characters. Warning: it is noteworthy that the SAX API does not nothing about the implementation of this event. In the case of a text scattered around tags girls tag underway, the reactions can be diverse. Thus the flow xml following:
<maBalise> a little
<baliseImbriquee nom="coucou"/> text <baliseImbriquee scattered nom="toto"/>
</ maBalise>

It can either give rise to three events containing the text "a little", "text", "scattered" is a single event containing the full text to say "a few scattered text." As the API does nothing, you will have to think about the fact that the parser that you have on hand may not be that of your customers and act accordingly, ie managing the two types of possible reactions so they provide the same conduct final in both cases.

ignorableWhiteSpace
Used to treat multiple spaces and tabs, since they normally have no value xml. One or two or 10 spaces, 1 space and a tab and 3 returns, etc. are spaces normally ignored in XML. This event is therefore lifted whenever spaces normally ignored met. In fact, the parameters of the method contain the complete chain of characters and indexes start and end of the series of spaces ignorables. It's up to you if you want to override the recommendation that considers these areas as being unnecessary.

skipped Entity
Avoid touching them, this method is raised each time an entity (a tag and the whole tree down) is ignored. It will if you asked the parser not validate the document and that the tag will be poorly trained. In short, you face a dangerous situation for your application, you then decide to leave on default values or, and this is usually the best, you stop treatment for failure in the environment.

Implementation of ContentHandler
Let us now turn to a small rapid implementation ContentHandler interface which remains central to implement to analyze a XML with SAX. As always, I give you an implementation in Java, which normally is easily adaptable to the various languages that you use. Our class implementation of ContentHandler therefore merely a document to analyze and prove he does well in expressing to the standard output (System.out.println () for non javayeurs) what it is in the process to do. I focus your attention on starting the flow of reading, reading simple entities or entities that are part of a namespace. Indeed, the rest becomes trivial once the groundwork has been included.
/ *
* Created on 2 November 03
*
* To change the template for this generated file go to
* Window> Preferences> Java> Code Generation> Code and Comments
* /
com.developpez.smeric.xml.sax package;
import org.xml.sax .*;
import org.xml.sax.helpers.LocatorImpl;
/ **
* @ Author smeric
*
* Example of implementation extremely simplified XML SAX a ContentHandler. The purpose of this example
* Is purely educational.
* Very simple sample implementation for XML SAX ContentHandler.
* /
public class SimpleContentHandler implements ContentHandler (
/ **
* Manufacturer by default.
* /
public SimpleContentHandler () (
super ();
/ / Define the locator by default.
locator = new LocatorImpl ();
)
/ **
* Definition of a locator which allows any time during the analysis, locate
* Treatment in the stream. The default locator indicates, for example, numero
* Line and the number of character on the line.
* @ Author smeric
* @ Param value to use the locator.
* @ See org.xml.sax.ContentHandler # setDocumentLocator (org.xml.sax.Locator)
* /
public void setDocumentLocator (Locator value) (
Locator = value;
)
/ **
* Event demarrage sent to parse the xml flow.
* @ Throws SAXException when probleme quelquonque not permitting
* Engage in the analysis of the document.
* @ See org.xml.sax.ContentHandler # startDocument ()
* /
public void startDocument () throws SAXException (
System.out.println ( "Top of the analysis of the document");
)
/ **
* Event sent at the end of the flow analysis xml.
* @ Throws SAXException when probleme quelquonque not permitting
* Consider the analysis of the document as being complete.
* @ See org.xml.sax.ContentHandler # endDocument ()
* /
public void endDocument () throws SAXException (
System.out.println ( "End of analysis of the document");
)
/ **
* Debut treatment in a namespace.
* @ Param prefix uses for this namespace in this part of the tree.
* @ Param URI namespace.
* @ See org.xml.sax.ContentHandler # startPrefixMapping (java.lang.String, java.lang.String)
* /
public void startPrefixMapping (prefix String, String URI) throws SAXException (
System.out.println ( "Treatment of namespace:" URI + + ", prefix chosen:" + prefix);
)
/ **
* End processing namespace.
* @ Param prefix the prefix has chosen the opening of the treatment of space Namespace.
* @ See org.xml.sax.ContentHandler # endPrefixMapping (java.lang.String)
* /
public void endPrefixMapping (String prefix) throws SAXException (
System.out.println ( "End processing namespace:" + prefix);
)
/ **
* Event has received whenever it encounters a tag analyzer xml opening.
* @ Param nameSpaceURI url namespace.
* @ Param localName the local name of the tag.
* @ Param rawName tag name in version 1.0 <code> nameSpaceURI + ":" + localName </ code>
* @ Throws SAXException if the tag does not correspond to what is expected,
* Such non-compliance with a dtd.
* @ See org.xml.sax.ContentHandler # startElement (java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
* /
public void startElement (String nameSpaceURI, String localName, String rawName, Attributes attributes) throws SAXException (
System.out.println ( "Opening of the tag:" + localName);
if (! "". equals (nameSpaceURI)) (/ / namespace particular
System.out.println ( "belonging to the namespace:" + nameSpaceURI);
)
System.out.println ( "Attributes of the tag:");
for (int index = 0; index <attributs.getLength (); index + +) (/ / you look down the list of attributes
System.out.println ( "-" + attributs.getLocalName (index) + "=" + attributs.getValue (index));
)
)
/ **
* Event each received a closing tag.
* @ See org.xml.sax.ContentHandler # endElement (java.lang.String, java.lang.String, java.lang.String)
* /
public void endElement (String nameSpaceURI, String localName, String rawName) throws SAXException (
System.out.print ( "Closing the tag:" + localName);

if (! "". equals (nameSpaceURI)) (/ / name space not null
System.out.print ( "belonging to the namespace:" + localName);
)
System.out.println ();
)
/ **
* Event has received whenever analyzer encounters characters (between
* Two tags).
* @ Param ch characters themselves.
* @ Param start the forefront of the first character in dealing effectively.
* @ Param end the rank of the last character in dealing effectively
* @ See org.xml.sax.ContentHandler # characters (char [], int, int)
* /
public void characters (char [] ch, int start, int end) throws SAXException (
System.out.println ( "# PCDATA:" + new String (ch, start, end));
)
/ **
* Received whenever the characters spacing can be ignored within the meaning of
* XML. That is to say that this event is sent to several areas is succeeding,
* Tabs, and returns to succedants and any combination thereof
* Three types of occurrence.
* @ Param ch characters themselves.
* @ Param start the forefront of the first character in dealing effectively.
* @ Param end the rank of the last character in dealing effectively
* @ See org.xml.sax.ContentHandler # ignorableWhitespace (char [], int, int)
* /
public void ignorableWhitespace (char [] ch, int start, int end) throws SAXException (
System.out.println ( "spaces unnecessary meetings: ..." + new String (ch, start, end) + "...");
)
/ **
* Meeting a direction of operation.
* @ Param target the target of the investigation into operation.
* @ Param data values associated to this target. In general, it arises in the form
* A series of name / value pairs.
* @ See org.xml.sax.ContentHandler # processingInstruction (java.lang.String, java.lang.String)
* /
public void processingInstruction (String target, String data) throws SAXException (
System.out.println ( "Operating Instructions:" + target);
System.out.println ( "whose arguments are:" + data);
)
/ **
* Received each time a tag is avoided in treating a cause of a
* Problem by not blocking the parser. For my part I do not think you
* In need in your treatment.
* @ See org.xml.sax.ContentHandler # skippedEntity (java.lang.String)
* /
public void skippedEntity (String arg0) throws SAXException (
/ / I do nothing, what happens is not really normal.
/ / To avoid this event, when it is best to specify a meme for your dtd
/ / Xml documents and have them validated by your parser.
)
Private Locator locator;
)

Start the analysis of an XML with our SAX parser
We have now responded to the pulses supplied by the parser sax but we still do not know instantiate a parser to point to our content manager, then ask him to start reading the feeds. So, let's see how we can we take. The handling is very simple, it requires an instance of XMLReader, to provide a URI to be analyzed, to provide our manager course and start reading. We have two ways to get to initialize our parser: a very simple but so little evolutionary and particularly closely linked to a publisher, for example:

XMLReader monAnalyseur = new org.apache.xerces.parsers.SAXParser ();
The alternative is to use a particular factory and the factory offered with the distrtibution official SAX: org.xml.sax.helpers.XmlReadersFactory.
Let us therefore now rapid implementation of a class of starting a reading of flux.
/ *
* Created on 2 November 03 with Eclipse for Java
* /
com.developpez.smeric.xml.sax package;
import java.io.IOException;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/ **
* This class is delivered unchanged.
* @ Author smeric
* @ Version 1.0
* /
public class (SimpleSaxParser
/ **
* Contructeur.
* /
public SimpleSaxParser (String uri) throws SAXException, IOException (
XMLReader saxReader = XMLReaderFactory.createXMLReader ( "org.apache.xerces.parsers.SAXParser");
saxReader.setContentHandler (new SimpleContentHandler ());
saxReader.parse (Uri);
)
public static void main (String [] args) (
if (0 == args.length | | 2 <args.length) (
System.out.println ( "Usage: SimpleSaxParser uri [parserClassName]");
System.exit (1);
)
String uri = args [0];
String parserName = null;
if (2 == args.length) (
parserName = args [1];
)
try (
SimpleSaxParser parser = new SimpleSaxParser (uri);
) Catch (Throwable t) (
t.printStackTrace ();
)
)
)
This gives as a result, launched on the roster of tests following:
<? xml version = "1.0" encoding = "ISO-8859-1"?>
<tests>
<test id="1" nom="mon test"/>
<test id="2" nom="test 2" type="rien"> A little text
</ test>
</ tests>

Debut of the analysis of material
Opening of the tag: tests
Attributes tag:
# PCDATA:

Opening of the tag: test
Attributes tag:
-- Id = 1
-- = Name my test
Closing tag: test
# PCDATA:

Opening of the tag: test
Attributes tag:
-- ID = 2
-- Name = test 2
-- Type = nothing
# PCDATA: A little word

Closing tag: test
# PCDATA:
Closing tag: tests
End of analysis of material

Conclusion
We are equipped to continue to study the implementation of our editor using the SAX API in Java. The API is used in various languages and porting this code is not complicated. The next step in this small courses episodes will therefore allow us to implement the time this API, to implement an editor and put themselves in the framework of the IOC.
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials