Class Example6triangle Extends DefaultHandler {// The current triangle local reference. . . Private Triangle CurrentTriangle; // Parent ... ContentHandler Parent; // XML parser XMLReader Parser; // Collect data from the "Characters" SAX event.
private CharArrayWriter contents = new CharArrayWriter (); public void collectTriangle (XMLReader parser, ContentHandler parent, Triangle newTriangle) {this.parent = parent; this.parser = parser; parser.setContentHandler (this); currentTriangle = newTriangle;} public void endElement (String namespaceURI, String localName, String qName) throws SAXException {if (localName.equals ( "x")) {currentTriangle.x = Integer.valueOf (. contents.toString () trim ()) intValue ();.} if (localname.equals ("y")) {currentTriangle.y = integer.Valueof (). Trim ()). INTVALUE ();} if (localname.equals ("width")) {CurrentTriangle.Width = INTEGER.VALUEOF (). TRIM ()). INTVALUE ();} if (localname.equals ("height")) {currentTriangle.Height = Integer.Valueof (). Valueof (). Trim ()). INTVALUE );} If ("Triangle")) {// converts the content processor (Content Handler) into Parent Parser.SetContentHandler (Parent);}} public void characters (char [] ch, int Start, INT Length) THROWS SAXEXCEPTION {// collects data from the "Characters" SAX event to the cache. Contents.Write (ch, start, length);}} Note We get the same output from the Shape class:
Example6: Circle: Circ1 x: 10 Y: 0 Width: 3 Height: 3triand: Tri1 x: 3 Y: 0 Width: 5 Height: 3TRIANGE: TRI2 x: 5 Y: 0 Width: 5 HEIGHT: 3 Conclusion We have a lot of advantages over the DOM API in the event of the SAX. We have involved some of the theories of XML documents that allow us to efficiently process simple and medium complexity. We also pointed out some places that use the SAX API to deal with it.
Finally, I hope that when you feel the SAX API when you feel the Dom API in a harsh environment.
In the next article, we will solve the problem of recursive XML structures, mixing labels, and SAX data navigation. These three buss run through a concentrated container management that can deal with the universal class libraries of the most complex XML mapping.
About the author Bob Hustead Has Been Wasting Way Too Much Time on Computers Since The Commodore 64 WAEN WAS Since The Commodore 64 WAEN WASTRODUCED. He is initially committed to the C language communication protocol and device driver development, and then transferred to the field of surface icon object design, first C later is Java. Later, it is committed to the middleware architecture. Bob is currently an enterprise application of underlying architects, which is effective in New York City AIG INSURANCE.
Resource
To download all the source code ZIP files for this article, please: http://www.javaworld.com/jw-08-2000/sax/jw-0804-sax.zip
For the documentation of SAX API 2.0, see "SAX 2.0 / Java Final Release": http://www.megginson.com/sax/java/index.html
Java 2 Performance and Idiom Guide, Craig Larman and Rhett Guthrie (Prentice Hall, Aug. 27, 1999): http://www.amazon.com/exec/obidos/ASIN/0130142603/qid=964730084/002-7840394-9715257
Useful information of other XMLs, see: http://www.xml.org
For the apache Xerces XML parser, please: http://xml.apache.org/xerces-j/index.html
Some nonsense (translators), so they want to translate this article because it is very small before, and it is free. It is a good time to translate someone else to use the machine to translate. It is considered to be a rape for technical documents, so I will make a one "Let some people look at what is called". " In fact, there is no simple ingredient. Suddenly, the work suddenly, a few want to stop, but have said to the original author to make it, although others will not pay attention to our small characters, I always feel that I don't want to go. It looks now, and I have to do it, I really don't easily do an article. In fact, I have always hope that there are many photocopy of the version - and save money. I really want to find a study, I have to read English, the technical document is really difficult than other English documents. As long as I have been scared, I will scare J I have a second part, if everyone I will be hard, and of course, it is best to help.