Á¦8Àå. SAX (Simple API for XML)

´ëü API Çʿ伺

À̺¥Æ® ±â¹Ý ÀÎÅÍÆäÀ̽º

SAX À̺¥Æ® ÇÁ·Î±×·¡¹Ö

¼³Ä¡ ¹× ½ÇÇà

SAX ½ÃÀÛ ¿¹Á¦

¸®½ºÆ® 8.2 [p.252] ¼öÁ¤ (Cheap1.java )
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class Cheap1 extends DefaultHandler
{
  protected double min = Double.MAX_VALUE;
  protected String vendor = null;  // ÃÖÀú°¡, ȸ»ç¸í

  /* startElement event */
  public void startElement(String uri, String lname,
                            String name, Attributes atlist)
  {
    if (name.equals("price"))
    {
      String attr = atlist.getValue("price");
      if (attr != null) {
          double price = toDouble(attr);
          if (min > price) {
              min = price;
              vendor = atlist.getValue("vendor");
        }
      }
    }
  }
  /* helper method */
  protected double toDouble(String str)
  {
    Double strDouble = Double.valueOf(str);
    if (strDouble != null) 
       return strDouble.doubleValue( );
    else  return 0.0;
  }
  /* property access : ȸ»ç¸í, ÃÖÀú°¡ */
  public String getVendor( ) 
  { return vendor; }
  public double getMin( ) 
  { return min; }

  /* main() for command-line & parser ½ÇÇà */
  public static void main(String[] args) 
                                      throws Exception
  {
    if (args.length < 1)
    {
      System.out.println("java Cheap1 filename");
      return;
    }

    // event handler »ý¼º
    Cheap1 cheap = new Cheap1( );

    // parser factory ¹× parser »ý¼º
    SAXParserFactory factory =  
          SAXParserFactory.newInstance( );
    SAXParser parser = factory.newSAXParser( );

    // parser È£Ãâ/½ÇÇà
    parser.parse(args[0], cheap);

    // °á°ú Ãâ·Â
    System.out.println("ÃÖÀú°¡°ÝÀº " + 
            cheap.getVendor() + " ȸ»çÀÇ $" +
            cheap.getMin() + " ÀÌ´Ù.");
  }
}

ÁÖ¿ä ÀÎÅÍÆäÀ̽º ¹× ¸Þ¼Òµå

¹®¼­ º¯È¯ ¿¹Á¦

(conv.java )
import javax.xml.parsers.*; ... // À§¿Í µ¿ÀÏ
... 
public class conv extends DefaultHandler
{
  protected int indent = 0; // indentation level

  public void setDocumentLocator(Locator loc)
 
{
    System.out.println("[ÆÄÀÏÁÖ¼Ò] " +
                            loc.getSystemId());
  }

  public void startDocument( )
  {  System.out.print("[level]");  }

  public void endDocument( )
  {  System.out.println("\n===============");  }

  public void startElement(String uri, String lname, 
                  String name, Attributes atlist)
  {
    String str = "[" + indent + "] " + name;
    int leng = atlist.getLength();
    if (leng > 0) 
       for (int i=0; i<leng; i++) 
          str += " ("+i+")"+atlist.getQName(i)+"=\"" 
                               +atlist.getValue(i)+"\"";
    System.out.print("\n"+str);
    indent++;
  }

  public void endElement(String uri, String lname, 
                                   String name)
  {    indent--;  }
  public void characters(char[] ch, int start, int length)
  {
    String str="";
    if (length == 0) return;
    for (int i=0; i<length; i++)
        if (ch[start+i] != '\n') str += ch[start+i];
    str = str.trim();
    if (str != "")
        System.out.print(" {" + str + "} ");
  }

  public void processingInstruction(String target, String data)
  {
    System.out.print("\n<? " + target + data + " ?>");
  }

  public static void main(String[] args)
                                      throws Exception
  {
    if (args.length < 1) {
        System.out.println("java conv filename");
        return;
    }
    conv con = new conv( );
    SAXParserFactory factory =  
            SAXParserFactory.newInstance( );
    SAXParser parser = factory.newSAXParser( );

    parser.parse(args[0], con);
  }
}
½ÇÇà : 
java  conv  price.xml                                         java  conv  prices.xml
[ÆÄÀÏÁÖ¼Ò] file:/C:/Lec/Ex-08/price.xml
[level]
[0] product
[1] name {XML Training}
[1] price (0)price="999.0" (1)vendor="Playfield"
[1] price (0)price="699.0" (1)vendor="XMLi"
[1] price (0)price="799.0" (1)vendor="WT"
[1] price (0)price="1999.0" (1)vendor="EM"
===============
[ÆÄÀÏÁÖ¼Ò] file:/C:/Lec/Ex-08/prices.xml
[level]
[0] products {}
[1] product {}
[2] name {XML Editor} {}
[2] price (0)currency="usd" {499.0} {} {}
[1] product {}
[2] name {DTD Editor} {}
[2] price (0)currency="usd" {199.0} {} {}
...
===============