Creating a Graphical Browser

In the previous chapter, I adapted the DOM parser browser that we wrote to display circles. It will be instructive to do the same here for the SAX parser browser because it will show how to retrieve specific attribute values. Here's the document that this browser might readin this chapter, I'll call this document ch12_06.xml. As in the previous chapter, I'm specifying the (x, y) origin of each circle and the radius of the circle as attributes of the <CIRCLE> element:

Listing ch12_06.xml
 <?xml version = "1.0" ?> <!DOCTYPE DOCUMENT [ lt;!ELEMENT DOCUMENT (CIRCLEELLIPSE)*> <!ELEMENT CIRCLE EMPTY> <!ELEMENT ELLIPSE EMPTY> <!ATTLIST CIRCLE     X CDATA #IMPLIED     Y CDATA #IMPLIED     RADIUS CDATA #IMPLIED> <!ATTLIST ELLIPSE     X CDATA #IMPLIED     Y CDATA #IMPLIED     WIDTH CDATA #IMPLIED     HEIGHT CDATA #IMPLIED> ]> <DOCUMENT>     <CIRCLE X='200' Y='160' RADIUS='50' />     <CIRCLE X='170' Y='100' RADIUS='15' />     <CIRCLE X='80' Y='200' RADIUS='45' />     <CIRCLE X='200' Y='140' RADIUS='35' />     <CIRCLE X='130' Y='240' RADIUS='25' />     <CIRCLE X='270' Y='300' RADIUS='45' />     <CIRCLE X='210' Y='240' RADIUS='25' />     <CIRCLE X='60' Y='160' RADIUS='35' />     <CIRCLE X='160' Y='260' RADIUS='55' /> </DOCUMENT> 

Here the trick will be to recover the values of the attributes X , Y , and RADIUS . I'll store those values in arrays named x , y , and radius . It turns out that getting an element's attribute values is easier using Java's SAX parser than it is with the DOM parser. The startElement method is passed an object of Attributes interface; all you have to do is to use that object's getValue method, passing it the name of the attribute you're interested in:

 import java.awt.*;  import java.awt.event.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.*; import java.io.*; public class ch12_07 extends DefaultHandler {  static int numberFigures = 0;   static int x[] = new int[100];   static int y[] = new int[100];   static int radius[] = new int[100];  .     .     .     public void startElement(String uri, String localName, String qualifiedName, graphics/ccc.gif Attributes attrs)     {         if (qualifiedName.equals("CIRCLE")) {  x[numberFigures] = Integer.parseInt(attrs.getValue("X"));   y[numberFigures] = Integer.parseInt(attrs.getValue("Y"));   radius[numberFigures] = Integer.parseInt (attrs.getValue("RADIUS"));   numberFigures++;  }     }     .     .     .     public static void main(String args[])     {     .     .     .     } } 

Having stored all the circles' data, I display them in the AppFrame class as we did in the previous chapter:

 class AppFrame extends Frame  {     int numberFigures;     int[] xValues;     int[] yValues;     int[] radiusValues;     public AppFrame(int number, int[] x, int[] y, int[] radius)     {         numberFigures = number;         xValues = x;         yValues = y;         radiusValues = radius;     }     public void paint(Graphics g)     {         for(int loopIndex = 0; loopIndex < numberFigures; loopIndex++){             g.drawOval(xValues[loopIndex], yValues[loopIndex], radiusValues[loopIndex], graphics/ccc.gif radiusValues[loopIndex]);         }     } } 

And that's all it takes. You can see the results in Figure 12-4, where the browser is displaying circles.xml.

Figure 12-4. Creating a graphical XML browser using a SAX parser.

graphics/12fig04.gif

Here's the code for this example:

Listing ch12_07.java
 import java.awt.*; import java.awt.event.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.*; import java.io.*; public class ch12_07 extends DefaultHandler {     static int numberFigures = 0;     static int x[] = new int[100];     static int y[] = new int[100];     static int radius[] = new int[100];     public void displayDocument(String uri)     {         DefaultHandler handler = this;         SAXParserFactory factory = SAXParserFactory.newInstance();         try {             SAXParser saxParser = factory.newSAXParser();             saxParser.parse(new File(uri), handler);         } catch (Throwable t) {}     }     public void startElement(String uri, String localName, String qualifiedName, graphics/ccc.gif Attributes attrs)     {         if (qualifiedName.equals("CIRCLE")) {             x[numberFigures] = Integer.parseInt(attrs.getValue("X"));             y[numberFigures] = Integer.parseInt(attrs.getValue("Y"));             radius[numberFigures] = Integer.parseInt (attrs.getValue("RADIUS"));             numberFigures++;         }     }     public void warning(SAXParseException exception)     {         System.err.println("WARNING! " +             exception.getMessage());     }     public void error(SAXParseException exception)     {         System.err.println("ERROR! " +             exception.getMessage());     }     public void fatalError(SAXParseException exception)     {         System.err.println("FATAL ERROR! " +             exception.getMessage());     }     public static void main(String args[])     {         ch12_07 obj = new ch12_07();         obj.displayDocument(args[0]);         AppFrame f = new AppFrame(numberFigures, x, y, radius);         f.setSize(400, 400);         f.addWindowListener(new WindowAdapter() {public void             windowClosing(WindowEvent e) {System.exit(0);}});         f.show();     } } class AppFrame extends Frame {     int numberFigures;     int[] xValues;     int[] yValues;     int[] radiusValues;     public AppFrame(int number, int[] x, int[] y, int[] radius)     {         numberFigures = number;         xValues = x;         yValues = y;         radiusValues = radius;     }     public void paint(Graphics g)     {         for(int loopIndex = 0; loopIndex < numberFigures; loopIndex++){             g.drawOval(xValues[loopIndex], yValues[loopIndex], radiusValues[loopIndex], graphics/ccc.gif radiusValues[loopIndex]);         }     } } 


Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net