XML Java Analysis (1) (6A)

zhaozj2021-02-17  51

The following code illustrates this ContentHandler rotation technology. Contents cache is shared by Class EXAMPLE6 and other specific ContentHandler:

Import org.xml.sax. *; import org.xml.sax.helpers. *; import java.io. *; import java.util. *; import common. *; public class example6 extends defaulthandler {// XML parsing. . . XmlReader Parser; // is parsed. . . Example6circle CircleMapper = new example6circle (); example6square Squaremapper = new example6square (); example6triangle trianglemapper = new example6triangle (); // Different or different local vector tables. . . Private Vector Circles = New Vector (); Private Vector Triangles = New Vector (); Private Vector Squares = New Vector (); // Collect data from the "Characters" SAX event. Private CharRayWriter Contents = New CharaRrayWriter (); // The XML parser is the constructor of the parameter. . . Example6 (XMLReader Parser) {this.Parser = Parser;} // Reserved the method of intercepting the SAX event. // / / Refer to Org.xml.sax.contenthandler.

// public void startElement (String namespaceURI, String localName, String qName, Attributes attr) throws SAXException {contents.reset (); if (localName.equals ( "Circle")) {Circle aCircle = new Circle (); aCircle.name = Attr.GetValue ("name"); circles.addelement (acircle); circleMapper.collectcircle (Parser, this, acircle);} if (localname.equals ("square") {square asquare = new square (); asquare .name = attr.getValue ("name"); squares.addelement (asquare); Squaremapper.collectsquare (Parser, this, asquare);} if (localname.equals ("triangle") {Triangle ATRIANGLE = New Triangle () Atriangle.name = attr.getValue ("name"); triangles.addelement (ATRIANGLE); TriangleMapper.CollectTriangle (Parser, this, Atriangle);}} public void endelement (String Namespaceuri, String localname, string qname) The Example 6 mapper is required in the THROWS SAXEXCEPTION {// endElement SAX event. } Public void characters (char [] ch, int start, int length) throws saxException {// writes the data content to the cache.

转载请注明原文地址:https://www.9cbs.com/read-30172.html

New Post(0)