Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


About SAX & SAX programming with Java
 
Æļ­°¡ ´ëü ¹«¾ùÀ̸ç, SAX(Simple API for XML)°¡ µµ´ëü ¹«¾ùÀÎÁö ¾Ë¾Æº¸°í, JAXP¿Í Xerces¸¦ ÀÌ¿ëÇÑ SAX1.0, 2.0ÇÁ·Î±×·¡¹ÖÀº ¾î¶»°Ô ÇÏ´ÂÁö ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ. ( 2003/03/12 ) 385
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

 XMLÀÇ ±âº»°³¿ä¿¡ ´ëÇÑ ¾ÆƼŬÀ» Àо¾Ò´Ù¸é ÀÌÁ¦ Simpe API form XMLÀÎ SAXÀÇ °³¿ë¿Í
 ÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.

 SAX°¡ ¾î¶² °ÍÀÌ´Ù, ¾î¶»°Ô ÀÛµ¿ÀÌ µÈ´Ù¶ó´Â °ÍÀ» ¾Ë°í ÀÖ´Â ´ç½ÅÀ̶ó¸é °ú°¨È÷ ´Ù¸¥ ¾ÆƼŬ·Î
 ¶°³ª¶ó~~

 ÀÚ, ±×·³ SAXÀÇ ±âº»°³¿ä¿Í °£´ÜÇÑ ÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.

 ¢º About SAX

    - Simple API for XML Parsing
    - ±âÁ¸ÀÇ event±â¹Ý Æļ­ÀÇ ¹®Á¦
    - °øÅëAPIÀÇ Çʿ伺 ´ëµÎ
    - 1998³â 5¿ù XML-DEV¿¡ ÀÇÇÑ SAX1.0¹ßÇ¥
    - Namespace Ãß°¡¿¡ µû¸¥ SAX API¼öÁ¤
    - 2000³â 5¿ù SAX2.0¹ßÇ¥

±âÁ¸ÀÇ XMLÀ» ÆĽÌÇϱâ À§ÇÑ µµ±¸µéÀÌ ¸¹¾ÆÁö¸é¼­ ¼­·Î ´Ù¸¥ ´Ù¾çÇÑ Æļ­µéÀ» ÀÌ¿ëÇÏ¿© °³¹ßÇÏ·Á´Â 
°³¹ßÀÚµéÀÇ ¾î·Á¿òÀÌ Áõ´ëµÇ´Â ¹®Á¦Á¡À» ¾È°í ÀÖ¾ú´Âµ¥, ÀÌ·¯ÇÑ ¿©·¯ Æļ­µéÀÌ °øÁ¸ÇÏ´Â 
°¡¿îµ¥¿¡¼­ °øÅëÀÇ API°¡ ÇÊ¿äÇÏ´Ù´Â ÀνĿ¡¼­ XML-DEV ¸ÞÀϸµ ¸®½ºÆ®ÀÇ °³¹ßÀÚµéÀÌ ¸ð¿©
1998³â 5¿ù SAX1.0À» ¹ßÇ¥ÇÑ´Ù.

XML ½ºÆå¿¡ À̸§Ãæµ¹·Î ÀÎÇÑ namespace(ÀÌÀü ¾ÆƼŬ¿¡¼­ ¼³¸í)ÀÇ ±â´ÉÀÌ Ãß°¡µÇ¸é¼­ ±âÁ¸ÀÇ 
SAX°¡ ÀÌ·¯ÇÑ namespace¸¦ Áö¿øÇÒ ¼ö ÀÖµµ·Ï 2000³â 5¿ù SAX2.0À» »õ·ÎÀÌ ¹ßÇ¥ ÇÑÃþ ´õ Áøº¸½ÃÅ°°Ô µÈ´Ù

ÀÌ·¯ÇÑ SAX2.0Àº SAX1.0ÀÌ °®Áö ¸øÇß´ø namespace¿Í skippend entity¿¡ ´ëÇÑ Áö¿øÀ» ÀÌ·ç°Ô ÇÏ¿´°í
±âÁ¸ÀÇ ÀÚ¹ÙÃø¸é¿¡¼­ SAX1.0À» SAX2.0ó·³ »ç¿ëÇϱâ À§ÇÑ XMLReaderAdapter Ŭ·¡½º¿Í SAX2.0À» SAX1.0ÀÇ 
Æļ­Ã³·³ ÀÛµ¿½ÃÅ°±â À§ÇÑ ParseAdapterŬ·¡½º°¡ Á¸ÀçÇÏ¿© À̵é 1.0°ú 2.0ÀÇ Â÷ÀÌ¿¡¼­ ¿À´Â 
¹®Á¦Á¡µéÀ» ÇØ°áÇس»°í Àִ Ư¡À» °¡Áö°í ÀÖ´Ù.

 ¢º µµ´ëü Æļ­¶õ ¹«¾ùÀ» ¸»Çϴ°¡

´Ü¾î¸¸ ³ª¿­ÇÑ´Ù°í Çؼ­ ÄÄÇ»ÅÍ°¡ ±×°ÍÀ» ¹®ÀåÀ¸·Î ÀνÄÇÏ´Â °ÍÀÌ ¾Æ´Ï´Ù. ¹®¹ý¿¡ ¸Â°Ô ±¸¼ºµÇ°í
¶ÇÇÑ ´Ü¾î°£ÀÇ À¯±âÀûÀÎ °ü°è°¡ ¼º¸³ÇؾßÇÔ ºñ·Î¼Ò Àǹ̸¦ °¡Áø ¹®ÀåÀÌ µÇ´Â °ÍÀÌ´Ù. 
¶ÇÇÑ Àǹ̸¦ °®°í ÀÖ´Ù´Â °Íº¸´Ù Áß¿äÇÑ °ÍÀº ÀÌ°ÍÀ» ÄÄÇ»ÅÍ¿¡°Ô ÀÌÇؽÃÅ°´Â ÀÏÀÌ´Ù. 
¾Æ¹«¸® ÁÁÀº Àǹ̸¦ °¡Áø ¹®ÀåÀÌ¶óµµ ¿ì¸®¿¡°Ô´Â ±Ã±ØÀûÀ¸·Î ÄÄÇ»ÅÍ°¡ ÀÌÇØÇÏÁö ¸øÇϸé 
¾Æ¹« ¼Ò¿ëÀÌ ¾ø´Â °ÍÀÌ´Ù. 

ÄÄÇ»ÅÍ ÀÔÀå¿¡¼­ Á¶±Ý ´õ À̾߱âÇÏÀÚ¸é 

    - ÀÔ·ÂµÈ ÅؽºÆ®¸¦ ¾îÈÖ ºÐ¼® ¹× ±¸¹® ºÐ¼®ÇØ ÁÖ´Â ÇÁ·Î±×·¥ 
    - ÀÔ·ÂµÈ ¹®ÀåÀÌ ¹®¹ý¿¡ ¸Â´Â°¡ ¿©ºÎ¸¦ °áÁ¤ÇØÁØ´Ù. 
    - ¹®¹ý¿¡ ¸Â´Â °æ¿ì ´Ù¸¥ Ãß°¡ ±â´ÉÀ» ÇÒ ¼öµµ ÀÖ´Ù. (ÆĽº Æ®¸® »ý¼º, °è»ê, °Ë»ö µîµî) 

µîÀÇ ÀÏÀ» ó¸®ÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ°Ô²û ¸¸µé¾îÁÖ´Â ÇÁ·Î±×·¥ÀÌ¶ó º¸¸é µÈ´Ù.

 ¢º SAXÀÇ Æ¯Â¡

SAX´Â öÀúÇÑ À̺¥Æ® ±â¹Ý ÀÎÅÍÆäÀ̽ºÀÌ´Ù. °´Ã¼ ±â¹Ý ÀÎÅÍÆäÀ̽ºÀÎ DOM(Document Object Model)ÀÌ 
¹®¼­ÀÇ ¸ðµç Á¤º¸ ³ëµå¸¦ ¸Þ¸ð¸®»ó¿¡ ÀúÀåÇØ¾ß ÇϹǷΠ¸¹Àº ¸Þ¸ð¸® ¿ë·®ÀÇ ¼Ò¸ð¿Í ÀÌ·Î ÀÎÇÑ 
¼öÇà ¼Óµµ°¡ ´Ù¼Ò ÀúÇ쵃 ¼ö ÀÖ´Â ¹Ý¸é, SAX´Â Æļ­°¡ ¹®¼­¸¦ ÆĽÌÇÏ´Â µ¿¾È¿¡ ¹ß»ýÇÏ´Â À̺¥Æ®¿¡ 
´ëÇؼ­¸¸ ¹ÝÀÀÇÏ°í À̸¦ µû·Î ¸Þ¸ð¸®¿¡ ÀúÀåÇÏÁö ¾ÊÀ¸¹Ç·Î ºü¸¥ ¼öÇà¼Óµµ¸¦ °¡Áø´Ù. 

±×·¯³ª ¹®¼­Á¤º¸¿¡ ´ëÇÑ À̺¥Æ® ¹ß»ýÀÌ Áö³ª°¡¸é ÀÌ¿¡ ´ëÇÑ Á¤º¸¸¦ ÀúÀåÇÏÁö ¾ÊÀ¸¹Ç·Î ÀÀ¿ëÇÁ·Î±×·¥¿¡¼­ 
ó¸®ÇØ¾ß ÇÒ ºÎºÐÀÌ »ó´ëÀûÀ¸·Î DOM º¸´Ù º¹ÀâÇØ Áú ¼ö ÀÖ´Ù´Â ´ÜÁ¡ÀÌ ÀÖ´Ù. 
ÀÌ·¯ÇÑ Á¡ ¶§¹®¿¡ DOM°ú SAX´Â ±× ÀÀ¿ëºÐ¾ß°¡ ±¸ºÐµÇ°í ÀÖ´Ù.
SAX´Â ¹®¼­ÀÇ ³»¿ëÀ» »ý¼º,¼öÁ¤ ÇÏ´Â ÀÀ¿ëÇÁ·Î±×·¥¿¡¼­´Â Àß ¾²ÀÌÁö ¾ÊÀ¸¸ç ÁÖ·Î ´ë¿ë·®ÀÇ 
¹®¼­ Á¤º¸¸¦ ºü¸¥ ½Ã°£ ¾È¿¡ ÀÐ¾î µéÀÌ´Â ÀÛ¾÷¿¡ ÁÖ·Î ÀÌ¿ëµÇ°í ÀÖ´Ù.

°´Ã¼ ±â¹Ý ÀÎÅÍÆäÀ̽º¿Í À̺¥Æ® ±â¹Ý ÀÎÅÍÆäÀ̽ºÀÇ Ä¿´Ù¶õ Â÷ÀÌÁ¡Àº À̺¥Æ®±â¹Ý ÀÎÅÍÆäÀ̽º(SAX)´Â
 ¹®¼­ ¿ä¼Ò¿¡ ´ëÇØ Æ®¸®±¸Á¶¸¦ »ý¼ºÇÏÁö ¾Ê´Â´Ù´Â °ÍÀÌ´Ù. ÀÌ·¯ÇÑ Á¡À¸·Î ÀÎÇØ ÀÀ¿ëÇÁ·Î±×·¥¿¡¼­´Â 
 À̺¥Æ®¿¡¼­ ¹ß»ýÇÑ Á¤º¸¿¡ ´ëÇÑ Ã³¸®°¡ ´Ù¼Ò º¹ÀâÇØÁú ¼ö ÀÖ´Ù. 

À̺¥Æ® ±â¹Ý ÀÎÅÍÆäÀ̽º´Â °´Ã¼ ±â¹Ý ÀÎÅÍÆäÀ̽ºº¸´Ù ÇÏÀ§ ·¹º§¿¡ ¼ÓÇÑ´Ù.    


 ¢º ÇöÀç Java SAX ParserÀÇ Á¾·ù¿¡´Â ¾î¶² °ÍµéÀÌ Àִ°¡?

   - Xerces
        Apache Foundation, DOM & SAX
   - Crimson
        Sun Project X, JAXP Default Parser
   - JAXP
        Sun, Java API for XML Processing

¸ÕÀú Xerces´Â SAX¿Í DOMahen¸¦ ´Ù·ê¼ö ÀÖ°ÔÇÏ´Â XML API¸¦ À§ÇÏ¿© Apache GroupÀÇ ÇÁ·ÎÁ§Æ®ÀÌ´Ù. 
ÇöÀç SAX1.0°ú SAX2.0 APIÀÇ ÀÎÅÍÆäÀ̽º°¡ Áö¿øÀÌ µÇ°í ÀÖÀ¸¸ç, XercesÀÇ SAXÆÐÅ°Áö´Â 
org.apache.xerces.parser¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.

CrimsonÀº SunÀÇ ProjectXÀÇ ÀÏȯÀ¸·Î JAXPÀÇ default parser·Î äÅõǾîÁ® ÀÖÀ¸¸ç XMLReaderÀÎÅÍÆäÀ̽º¸¦ 
±¸ÇöÇϹǷΠSAX API¸¦ ÀÌ¿ëÇÏ¿© ¿©·¯°¡Áö ´Ù¾çÇÑ ÆḺ̌â´ÉÀ» »ç¿ëÇÒ ¼ö Àִ Ư¡À» °¡Áö°í ÀÖ´Ù.

JAXP´Â Xercesó·³ DOM°ú SAXµÑ´Ù¸¦ »ç¿ëÇÒ ¼ö ÀÖµµ·Ï Áö¿øÇØÁÖ´Â APIÀÌ´Ù. 
ÇÑ°¡Áö ´Ù¸¥ Á¡ÀÌ ÀÖ´Ù¸é SAX APIÀÇ ÀÎÅÍÆäÀ̽º¸¦ Á÷Á¢±¸ÇöÇÑ Å¬·¡½º¸¦ Á¦°øÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó 
abstract°èÃþÀ» ÇÑ´Ü°è ´õ ¾ñÀº ÇüŸ¦ Á¦°øÇÑ´Ù. 
Áï °³¹ßÀÚ´Â SAX API¿¡ Á÷Á¢ Á¢±ÙÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó Æļ­ÀÇ Á¾·ù¿¡ µû¶ó µ¶¸³ÀûÀÎ ÇÁ·¹ÀÓ¿÷¿¡ 
Á¢±ÙÇÑ´Ù°í º¸´Â ÆíÀÌ ÁÁÀ» °ÍÀÌ´Ù.

ÀÚ ±×·¯¸é ¾Æ·¡¿¡¼­ Äڵ带 º¸¿©ÁÖµµ·Ï Çϰڴµ¥ ù¹ø°·Î Sun microsystemsÀÇ Default ParserÀÎ 
JAXP¸¦ ÀÌ¿ëÇÏ¿© °£´ÜÇÑ SAX2 javaÇÁ·Î±×·¥À» ÀÛ¼ºÇØ º¸µµ·Ï ÇÑ´Ù.
¶ÇÇÑ Xerces¸¦ ÀÌ¿ëÇÏ¿© º¯È¯°¡´ÉÇÑ SAX1, SAX2 ÇÁ·Î±×·¥À» ÀÛ¼ºÇغ¸µµ·Ï ÇÑ´Ù.
SAX2¸¦ ÀÌ¿ëÇÏ¿© ½Ç¹«¿¡ Àû¿ëÇÒ ¼ö ÀÖ´Â xml°´Ã¼ÀÇ java objectº¯È¯À» ÄÚµå·Î »ìÆ캸µµ·Ï ÇÑ´Ù.

¸ÕÀú °¢°¢¿¡ ´ëÇÑ Æļ­µéÀ» ´Ù¿î¹Þ¾Æ ¿©·¯ºÐµéÀÇ ÄÄÇ»ÅÍ¿¡ ¼³Ä¡(»ç½Ç ¼³Ä¡°¡ ¾Æ´Ï¶ó ¾ÐÃàÀ» Ǭ ÈÄ 
Ŭ·¡½º Æнº¿¡ Ãß°¡½ÃÅ°´Â ÀÏÀÌ¶ó º¸´Â°ÍÀÌ ¹«¹æÇÏ°Ú´Ù)Çϵµ·Ï ÇÑ´Ù.


 ¢º ¾îµð¼­ SAXÆļ­µéÀ» ´Ù¿î¹ÞÀ» ¼ö Àִ°¡?

Xerces : http://xml.apache.org/xerces2-j/index.html
Crimson : http://xml.apache.org/crimson/index.html
JAXP : http://java.sun.com/xml

À§ÀÇ »çÀÌÆ®¿¡¼­ ´Ù¿î·Îµå ¸Þ´º¸¦ ÀÌ¿ëÇÏ¿© °¢°¢ÀÇ Æļ­µéÀ» ¹Þ¾Æ ¿©·¯ºÐµéÀÇ ÄÄÇ»ÅÍ¿¡ XMLÀ̶õ 
µð·ºÅ丮¸¦ ¸¸µçÈÄ ¾ÐÃàÀ» Ç®µµ·Ï ÇÏÀÚ.

SAXÆļ­, öÀúÇÑ À̺¥Æ®±â¹ÝÀÇ Æļ­ÀÌ´Ù. ¿©·¯ºÐµéÀº AWT, Swing°°Àº eventÇڵ鸵 
Äڵ带 ÀÛ¼ºÇغ»ÀûÀÌ Àִ°¡? 

¹Ù·Î ÀÌ SAX´Â XMLÀÇ ¹®ÀåÀÌ Æļ­ÀÇ ÀÔ·ÂÀ¸·Î µé¾î¿À°ÔµÇ¸é °¢ tag, attribute, contentÀÇ ³»¿ëÀ» 
ÇϳªÀÇ ÀÔ·ÂÀ̺¥Æ®·Î ¹Ù¶óº¸°í, °¢ ¿ä¼ÒµéÀ» ¸¸³µÀ» °æ¿ì ÇØ´çÀ̺¥Æ®¸¦ ¿©·¯ºÐµéÀÌ ÀÛ¼ºÇÑ 
ÇÁ·Î±×·¥ÃøÀ¸·Î ³Ñ°ÜÁÖ°Ô µÈ´Ù.

1. °øÅë Çڵ鷯 Ŭ·¡½ºÀÇ ÀÛ¼º
Handler classÀÇ ÀÛ¼º
1:		/*
2:    * @(#)ParserHandler.java	1.00 2002.08.05
3:    *
4:    * Copyleft Carouser
5:    * 
6:    */
7:    package com.javapattern;
8:   
9:    import javax.xml.parsers.SAXParser;
10:   import javax.xml.parsers.SAXParserFactory;
11:   import org.xml.sax.*;
12:   import org.xml.sax.helpers.*;

/** Áß·« **/

22:   public class ParserHandler extends DefaultHandler{
23:   	private Locator loc;
24:   
25:   	public void startDocument() throws SAXException {
26:   	    System.out.println("startDocument method");
27:   	}
28:   	public void endDocument() throws SAXException {
29:   	    System.out.println("endDocument method");
30:   	}
31:   
32:   	public void characters(char[] ch, int start, int length) {
33:   	    System.out.println("Characters : " + new String(ch, start, length));
34:   	}
35:   public void startElement(String uri, String localName, 
                                              String qName, Attributes attr) {
36:   	System.out.println("Start Element : " + qName);	
37:   	int num = attr.getLength();
38:  	System.out.println("    Length  : " + num);
39:   
40:   	    //Display the attribute specification 
41:   	    for(int i = 0 ; i < num ; i++){
42:   		    System.out.println("    Name : " + attr.getLocalName(i));
43:   		    System.out.println("    Value : " + attr.getValue(i));
44:   	    }
45:   	}
46:   	public void endElement(String uri, String localName, String qName){
47:   	    System.out.println("End Element  : " + qName);
48:   	} // end of endElement
50:   } // end of class

Line 9, 10 : JAXP¸¦ ÀÌ¿ëÇÏ¿© À§ÇÏ¿© SAXParser¿Í SAXParserFactory¸¦ Ãß°¡½ÃŲ´Ù. JAXP´Â ¸ðµç java API¿¡ »ç¿ëµÇ´Â ÀϹÝÀûÀÎ ÆÐÅÏÀÎ factoryÆÐÅÏÀ» »ç¿ëÇÑ´Ù. µû¶ó¼­ À§ÀÇ SAXParserFactoryÀÇ Å¬·¡½º¸¦ »ç¿ëÇÏ¿© XMLÆĽ̿¡ °ü·ÃµÈ °´Ã¼µéÀ» »ý¼ºÇØ ³½´Ù. ¶ÇÇÑ À§ facltory Ŭ·¡½ºÀÇ newSAXParser()¸Þ¼Òµå¸¦ »ç¿ëÇϸé SAX2 Parser°´Ã¼¸¦ »ý¼ºÇØ ³¾ ¼ö ÀÖ´Â °ÍÀÌ´Ù. Line 11 : XML spec¿¡ Á¤ÀÇÇÑ interface¸¦ ¼±¾ðÇÑ´Ù. ¿©±â¼­´Â Attributes¿Í SAXParseExceptionµîÀ» »ç¿ëÇϱâ À§ÇÏ¿© import½ÃŲ´Ù. line 22 : Ŭ·¡½ºÀÇ ¼±¾ð, ÇöÀç ParserHandler°´Ã¼°¡ SAX2 event defaultÀÎ DefaultHandlerŬ·¡½ºÀÇ Å¸ÀÔÀÌ µÇµµ·Ï ¼±¾ðÇÑ´Ù. Çڵ鷯Ŭ·¡½ºÀÇ Á¾·ù¿¡´Â EntityResolver, DTDHandler, ContentHandler, ErrorHandler°¡ ÀÖ´Ù. Line 23: Document location°ú ÀÌ¿ëÇÏ¿© SAXÀÎÅÍÆäÀ̽º¸¦ ±¸¼ºÇÑ´Ù. Line 25~28 : startElement()¿Í endElement()¸Þ¼Òµå´Â document event parsingÀÇ ½ÃÀÛ°ú ³¡ÁöÁ¡¿¡¼­ È£ÃâµÈ´Ù. Line 32 : characters()¸Þ¼Òµå´Â ¹®Àڹ迭, ½ÃÀÛÀ妽º, ±æÀÌÀÇ 3°³ Àμö·Î ±¸¼ºÀÌ µÇ´Âµ¥ ¹®Àڹ迭Àº ÆĶó¹ÌÅÍ·Î Àü´ÞµÈ ¹®ÀÚÀ̸ç, ÀÌ·¯ÇÑ start¿Í length¸¸Å­À» ½ºÆ®¸µ ¹®ÀÚ¿­·Î º¯È¯½ÃÄÑ Ç¥ÇöÇÒ ¼ö Àִµ¥, ±× ºÎºÐÀÇ ¿ªÇÒÀ» ÇÑ´Ù Line 35 : Parser°¡ XML ElementÀÇ Ã¹±âÈ£ÀÎ ¡®<¡®¸¦ ¸¸³ª°Ô µÉ °æ¿ì È£ÃâµÇ´Â ¸Þ¼ÒµåÀε¥, ¿©·¯°³ÀÇ attributeµéÀÌ ÇϳªÀÇ elementµé¿¡ ¼ÓÇÒ ¼ö Àֱ⠶§¹®¿¡ Àμö·Î¼­ AttributesŬ·¡½ºÀÇ ÇüŸ¦ ¹ÞÀ» ¼ö Àִµ¥ ÀÌ´Â loopingÀ» ÅëÇÏ¿© ¾î¶² attributeµéÀÌ ÇØ´ç element¿¡ ¼ÓÇÏ´ÂÁö¸¦ Ãâ·ÂÇØ ³¾ ¼ö ÀÖ´Ù. Uri¿Í localNameÀº Æļ­°¡ namespace¸¦ ÀνÄÇÒ ¼ö ÀÖ´Â °æ¿ì¿¡¸¸ Àü´ÞÀÌ µÇ¸é qNameÀº ¾ðÁ¦³ª ÇØ´ç elementÀÇ À̸§À̾î¾ß ÇÑ´Ù. Line 46 : Parser°¡ elementÀÇ end tagÀÎ '</'¸¦ ¸¸³µÀ» °æ¿ì È£ÃâÀÌ µÈ´Ù. JAXP¸¦ ÀÌ¿ëÇÑ SAX2.0ÇÁ·Î±×·¡¹Ö
9:    import javax.xml.parsers.SAXParser;
10:   import javax.xml.parsers.SAXParserFactory;
11:   import org.xml.sax.*;
12:   import org.xml.sax.helpers.*;
13:   import java.io.File;
23:   public class JAXPSample {
24:   	public static void main(String [] args) throws Exception {
25:   	    SAXParserFactory factory = SAXParserFactory.newInstance();
26:   	    SAXParser parser = factory.newSAXParser();
27:   	    DefaultHandler handler = new ParserHandler(); 
28:   
29:   	    parser.parse(new File(args[0]), handler);
30:   	    }
31:   }

Line 9~13 : Program¿¡ ÇÊ¿äÇÑ package ¹× class¸¦ import½ÃŲ´Ù. Line 25 : SAX parsing¿¡ ÇÊ¿äÇÑ factoryŬ·¡½º¿¡ ´ëÇÑ ÀνºÅϽº¸¦ ¾òµµ·Ï ÇÑ´Ù. Line 26 : newSAXParser´Â javax.xml.parsers.SAXParserÀÇ Ãß»óŬ·¡½º¸¦ »ó¼Ó¹ÞÀº JAXPÀÇ °´Ã¼¸¦ ¹ÝȯÇØÁØ´Ù. Line 27 : ÀÌÀü ÆäÀÌÁö¿¡¼­ Á¤ÀÇÇÑ DefaultHandlerŬ·¡½º¸¦ Á¤ÀÇÇÏ¿© °¢°¢ÀÇ SAXÀ̺¥Æ®¸¦ ó¸®ÇÒ ¼ö ÀÖµµ·Ï user defineŬ·¡½ºÀÎ ParserHandlerÀÇ °´Ã¼¸¦ »ý¼ºÇϵµ·Ï ÇÑ´Ù. Line 29 : SAXParserÀÇ Çٽɸ޼ҵåÀÎ parse()¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿© ¹Þ¾ÆµéÀÎ ÆÄÀÏ°´Ã¼¸¦ ÆĽÌÇϵµ·Ï ÇÑ´Ù Xerces¸¦ ÀÌ¿ëÇÑ SAX2.0ÇÁ·Î±×·¡¹Ö
1:   package com.javapattern;
2:   
3:   import org.apache.xerces.parsers.SAXParser;
4:   import org.xml.sax.ContentHandler;
5:   import org.xml.sax.XMLReader;
6:   import org.xml.sax.InputSource;
7:   import java.io.FileReader;
8:   
9:   public class XercesSample {
10:   	public static void main(String [] args) throws Exception {
11:   	    //SAX1.0
12:       //Parser parser = new SAXParser();
13:   	    //SAX2.0
14:   	    XMLReader reader = new SAXParser();
15:   	    InputSource source = new InputSource(new FileReader(args[0]));
16:   	    ContentHandler handler = new ParserHandler(); 
17:   	    reader.setContentHandler(handler);
18:   	    reader.parse(source);
19:   	}
20:   }

À§ÀÇ ÇÁ·Î±×·¥Àº event handlerŬ·¡½º·Î¼­ ¾Õ¼­ Á¤ÀÇÇÑ ParserHandler Ŭ·¡½º¸¦ À̺¥Æ®Å¬·¡½º·Î µî·Ï½ÃÄÑ »ç¿ëÇϹǷΠÄÚµå´Â À­ºÎºÐ¿¡ ³ª¿ÍÀÖ´Ù. Line 12 : Xerces°°Àº °æ¿ì SAX1°ú SAX2¿¡ ¸ðµÎ Àû¿ë½Ãų ¼ö ÀÖ´Â APIÀÇ ¹­À½À¸·Î °¢°¢ÀÇ »óȲ¿¡ ¼­·Î ´Ù¸¥ Á¤ÀǸ¦ ³»¸± ¼ö Àִµ¥ ParserÀÎÅÍÆäÀ̽º¿¡ SAXParser¶ó´Â °´Ã¼¸¦ »ý¼ºÇÏ¿© ÇÒ´çÇÔÀ¸·Î¼­ SAX1.0¹öÁ¯ÀÇ parser¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. Line 14 : SAX2.0ÀÇ ÆĽ̿¡ »ç¿ëµÇ´Â Ŭ·¡½º·Î¼­´Â XMLReader¶ó´Â ÀÎÅÍÆäÀ̽º¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. Line 15 : ÆĽ̿¡ »ç¿ëµÉ InputSource°´Ã¼¸¦ »ý¼ºÇÏ°Ô µÇ´Âµ¥ À§ÀÇ Äڵ忡¼­´Â ÀԷ¹ÞÀº xmlÆÄÀÏÀ» ÀÌ¿ëÇÏ¿© ó¸®ÇÏ°Ô µÈ´Ù. Line 16, 17 : ¿ì¸®°¡ Á¤ÀÇÇÑ ParserHandler¸¦ ÀÌ¿ëÇÏ¿© °´Ã¼¸¦ »ý¼ºÇÑ ´ÙÀ½ 14¹ø¶óÀο¡¼­ Á¤ÀÇÇÑ xmlÆļ­ÀÇ handler·Î¼­ µî·ÏÇÏ°Ô ÇÑ´Ù. Line 18 : XMLReaderÀÇ parse()¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿© ó¸®ÇÑ´Ù. Sample XML¹®¼­
<?xml version="1.0" ?>
<Profile>
	<Person>
		<FirstName>JiWoong</FirstName>
		<LastName>Choi</LastName>
		<Age>30</Age>
		<Height>181</Height>
		<Job>Java Developer</Job>
	</Person>
	<Person>
		<FirstName>HongSeong</FirstName>
		<LastName>Jeon</LastName>
		<Age>33</Age>
		<Height>177</Height>
		<Job>Java Architect</Job>
	</Person>
</Profile>

½ÇÇà¹æ¹ý set SAMPLE_HOME=´ç½ÅÀÌ ¼³Ä¡ÇÑ XMLÆļ­¸¦ À§ÇÑ jarÆÄÀÏÀÌ ÀÖ´Â µð·ºÅ丮 javac -classpath .;%SAMPLE_HOME%\crimson.jar;%SAMPLE_HOME%\jaxp.jar; -d . %1 %2 %3 %4 %5 java -classpath .;%SAMPLE_HOME%\crimson.jar;%SAMPLE_HOME%\jaxp.jar; %1 %2 %3 %4 %5 À§¿Í °°Àº ¸í·É¾î¸¦ °®´Â shellÀ̳ª batchÆÄÀÏÀ» ¸¸µé¾î¼­ »ç¿ëÇϴ°ÍÀÌ ÈξÀ ¼ö¿ùÇÒ °ÍÀÌ´Ù. ÀÌ»óÀ¸·Î °£´ÜÇÏ°Ô SAXÀÇ °³¿ä¿Í ÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© º¸¾Ò´Ù. ÀÇ¿Ü·Î XMLÀº ½ÉÇÃÇÏ°Ô Á¢±ÙÇÒ ¼ö Àִ Ư¡À» °¡Áö°í ÀÖ´Ù. ¿äÁò¿¡´Â XML editorµéÀÌ Àß ³ª¿ÍÀ־ ±×·± °ÍµéÀ» ÀÌ¿ëÇÏ¿© ½±°Ô ÀÛ¼ºÇÒ ¼ö ÀÖÀ¸¸ç, ÀÌ·¯ÇÑ ³»¿ëÀ» ¾Ë°Ô µÈ´Ù¸é ÈξÀ ´õ È¿°úÀûÀ¸·Î ȯ°æ ¹× µ¥ÀÌÅÍ Àü¼ÛµîÀÌ »ç¿ëÇÒ ¼ö ÀÖ´Ù¶ó´Â °ÍÀ» ¸í½ÉÇÏÀÚ.
 
1
References
 
Xerces : http://xml.apache.org/xerces2-j/index.html
Crimson : http://xml.apache.org/crimson/index.html
JAXP : http://java.sun.com/xml
http://www.w3c.org This Article Source : SAXTest.zip
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD