XML's Java resolution (1) (5)

zhaozj2021-02-17  45

Next, we will come to the "violent" to distinguish the label name to map data to different objects:

Import org.xml.sax. *; import org.xml.sax.helpers. *; import java.io. *; import java.util. *; import common. *; public class example5 extends defaulthandler {// By rib The logo of capturing a tag content. Private Boolean Incircle = false; private boolean infraingle = false; private boolean insquare = false; // Different local vector tables. . . Private Vector Triangles = New Vector (); Private Vector Squares = New Vector (); Private Vector Circles = New Vector (); // The local reference of the current body. . . Private Triangle CurrentTriangle; Private Circle CurrentCriRCLE; Private Square Currentsquare; // Collects the cache of data from the "Characters" SAX event. Private CharRaywriter Contents = New CharaRrayWriter (); // Reserved the method of the DefaultHandler class to intercept 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")) {inCircle = true; currentCircle = new Circle (); currentCircle.name = attr.getValue ( "name"); circles.addElement (currentCircle);} if (localName.equals ( "Square")) {inSquare = true; currentSquare = new Square (); currentSquare.name = attr. getValue ( "name"); squares.addElement (currentSquare);} if (localName.equals ( "Triangle")) {inTriangle = true; currentTriangle = new Triangle (); currentTriangle.name = attr.getValue ( "name") Triangles.addelement (currenttriangle);}} public void endelement (String Namespaceuri, String Localname, String Qname) Thro WS SAXEXCEPTION {IF (localname.equals ("x")) {if (incircole) {currentcircle.x = integer.Valueof (Contents.Tostring (). Trim ()). INTVALUE ();} else if (insquare) { Currentsquare.x = integer.Valueof (Contents.Tostring (). Trim ()). INTVALUE ();} else {currentTriangle.x = integer.Valueof (). Trim ()). INTVALUE ();

}}} IF ("y")) {if (incircole) {currentcircle.y = integer.Valueof (Contents.Tostring (). Trim ()). INTVALUE ();} else if (insquare) {Currentsquare .y = integer.Valueof (). Trim ()). INTVALUE ();} else {currenttriangle.y = integer.Valueof (). Trim ()). IntValue ();}} IF ("Width") {if (incircole) {currentcircle.width = integer.valueof (). Trim ()). INTVALUE ();} else if (insquare) {currentsquare.width = INTEGER.VALU EOF (). Trim ()). INTVALUE ();} else {currentTriangle.Width = integer.Valueof (Contents.Tostring (). Trim ()). INTVALUE ();}}}} ("Height")) {if (incircole) {currentcircle.Height = Integer.Valueof (). Trim ()). INTVALUE ();} else if (insquare) {currentsquare.Height =

Integer.Valueof (). Trim ()). INTVALUE ();} else {currentTriangle.Height = Integer.Valueof (Contents.Tostring (). Trim ()). INTVALUE ();}}} . Equals ("circalname.equals (" tiCalname.equals ("triangle = false) {INSQUARE.EQUALS (" Triangle ") {INTRIANGLE = false;}} Void Characters (char [] ch, int start, int length) THROWS SAXEXCEPTION {// writes the data content to the cache.

contents.write (ch, start, length);} public Vector getCircles () {return circles;} public Vector getSquares () {return squares;} public Vector getTriangles () {return triangles;} public static void main (String [] Argv) {system.out.println ("eXample5:"); try {// Create a SAX 2 parser. . . XmlReader XR = XmlReaderFactory.createxmlReader (); // Install ContentHandler. . . EXAMPLE5 EX5 = New Example5 (); Xr.SetContentHandler (ex5); // parsing files. . . Xr.Parse (New FileRead (New FileReader)); // Displays the circular object to the standard output. . . Circle C; Vector Items = EX5.GETCIRCLES (); ENUMERATION E = items.elements (); while (E.hasMoreElements ()) {c = (circle) E.NEXTELEMENT (); c.Print (System.out); } // Display the rectangular object to the standard output. . . Square s; items = ex5.getsquares (); e = items.ersion (); while (E.hasMoreElements ()) {s = (square) E.NEXTELEMENT (); s.print (system.out);} / / Display the triangle object to the standard output. . . Triangle T; items = ex5.gettriangles (); e = items.ersion (); while (e.hasmoreElements ()) {t = (triangle) E.NEXTELEMENT (); t.print (system.out);} Catch (Exception E) {E.PrintStackTrace ();}}} The following is the output we collect data in our form class:

Example5: Circle: Circ1 x: 10 Y: 0 Width: 3 Height: 3triange: Tri1 x: 3 Y: 0 Width: 5 Height: 3TRIANGE: TRI2 x: 5 Y: 0 Width: 5 HEIGHT: 3 The second program uses you can replace the SAX Content of the SAX parser at runtime. This allows us to break the mapping task into a small module. We can implement the mapping code to a specific clip of a XML document.

The endelement () in the second example is not used to nested IF statements. This modular design is particularly effective when handling more complex XML documents. This method also solves the repeating labels that are not predicated in the XML document.

Although the second approach is slightly bloated because the repetition of most types definitions, this method of replacing the ContentHandler class is the first step toward a more general SAX parsing method. Replacing ContentHandler is also another way to change the beer mug in the faucet of the SAX parser (the translation: Robert is very likely a big belly image :-)).

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

New Post(0)