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

´ëü API Çʿ伺

¼³Ä¡ ¹× ½ÇÇà

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

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

SAX À̺¥Æ® Çڵ鷯

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

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

public class conv extends DefaultHandler
{
  private int nspace = 4; // indentation spaces  
  private int indent = 0;   // indentation level
  private boolean inTag=false;

  public static void main(String[] args) throws Exception
  {
    if (args.length < 1)
      System.out.println("...java reader1 filename");
      conv con = new conv( );
      con.read(args[0]);
   }

  public void read(String fileName) throws Exception
  {
    SAXParserFactory  factory =
               SAXParserFactory.newInstance( );
    SAXParser parser = factory.newSAXParser( );
    parser.parse(fileName, this);
  }

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


  public void startDocument( )
  {  System.out.print("===============");  }

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

  public String spaces(int num)
  {
    String str = "";
    if (num > 0)  for (int i=0; i<num; i++) str += " ";
    return str;
  }
  public void startElement(String uri, String lname, 
                  String qname, Attributes atts)
  {
     String str =
spaces(indent*nspace);
     if (inTag) System.out.println();
     System.out.print(str + "<" + qname);
     int leng =
atts.getLength();
     if (leng > 0)
       for (int i=0; i<leng; i++) System.out.print(" " +
         
atts.getQName(i)+"=\""+atts.getValue(i)+"\"");
     System.out.print(">");
     indent++;
     inTag=true;  
  }
  public void endElement(String uri,
                         String lname, String qname)
  {
     System.out.println("</" + qname + ">");
     indent--;
     inTag=false;
  }
  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+" ?>");
  }

}
½ÇÇà

C:>javac conv.java
C:>java conv booklist.xml
[ÆÄÀÏ] file:/C:/Lec/XML02/booklist.xml
===============
<? xml-stylesheet type="text/xsl" href="booklist.xsl" ?>
<list>
    <book InStock="2">
        <title>XML Bible</title>
        <author>Gwyneth Paltrow</author>
        <pages>652</pages>
        <price currency="usd">39.99</price>
</book>
    <book InStock="0">
        <title>XML Ŭ·¡½º</title>
        <author>ÀÓ¼ø¹ü</author>
        <pages>458</pages>
        <price>19000</price>
</book>
    <book InStock="8">
        <title>
            <subtitle>¿­Ç÷°­ÀÇ</subtitle>
XML By Example</title>
        <author>È«±æµ¿</author>
        <pages>529</pages>
        <price>25000</price>
</book>
</list>
=============== 

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

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