À̺¥Æ® ¹ß»ý ½ÃÁ¡ | À̺¥Æ® Çڵ鷯 |
¨ç <list>¨è <book InStock="0">¨é* <title>¨êXML Ŭ·¡½º¨ë</title>¨ì <author>¨íÀÓ¼ø¹ü¨î</author>¨ï <pages/>¨ð¨ñ** <price>¨ò19000¨ó</price>¨ô </book>¨õ </list>¨ç' ¨è' |
¨çstartDocument() ¨èstartElement() ¨éstartElement() *¼Ó¼ºÀº ¸Å°³º¯¼ö·Î Àü´Þ ¨êstartElement() ¨ëcharacters() ¨ìendElement() ¨ístartElement() ¨îcharacters() ¨ïendElement() ¨ðstartElement() ¨ñendElement() ** À̺¥Æ® 2ȸ ¨òstartElement() ¨ócharacters() ¨ôendElement() ¨õendElement() ¼Ó¼ºÀº ¸Å°³º¯¼ö·Î Àü´Þ ¨ç'endElement() ¨è'endDocument() |
import java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class MySAX { public static void main(String[] args) throws Exception { if (args.length < 1) ...¿¡·¯¸Þ½ÃÁö... SAXParserFactory factory = SAXParserFactory.newInstance( ); SAXParser parser = factory.newSAXParser( ); myHandler handler = new myHandler(); parser.parse(new FileInputStream(args[0]), handler); } } public class myHandler extends DefaultHandler { public void startDocument( ) { ... } public void startElement( ) { ... } public void endDocument( ) { ... } ... } |
ÇÊ¿äÇÑ
Ŭ·¡½º Æ÷ÇÔ Çڵ鷯 Ŭ·¡½º Á¤ÀÇ Å×½ºÆ® Ŭ·¡½º ¸ÞÀÎ parser factory »ý¼º parser »ý¼º event handler »ý¼º parser È£Ãâ/½ÇÇà½Ã event handler ÇÒ´ç Çڵ鷯 Ŭ·¡½º Á¤ÀÇ (DefaultHandler) ... ÇØ´ç ¸Þ¼Òµå ... |
ÁÖ¿ä ÀÎÅÍÆäÀ̽º Á¾·ù |
Áö¿øµÇ´Â ÁÖ¿ä ¸Þ¼Òµå |
|
XMLReader (p.471) |
parse(), getContentHandler(), getDTDHandler(), getEntityResolver(), getErrorHandler(), getFeature(), getProperty(), setContentHandler(), setDTDHandler(), setEntityResolver(), setErrorHandler(), setFeature(), setProperty() |
|
DefaultHandler
* |
startDocument(), endDocument(), setDocumentLocator(), ignorableWhitespace(), startElement(), endElement(), characters(), processingInstruction(), startPrefixMapping(), endPrefixMapping() |
|
* |
ContentHandler |
|
DTDHandler |
notationDecl(), unparsedEntityDecl() |
|
EntityResolver |
resolveEntity(), skippedEntity() |
|
ErrorHandler |
warning(), error(), fatalError() |
|
Attributes (p.483) |
getLength(), getQName(i), getLocalName(), getIndex(), getType(i), getValue(i), getURI() |
|
Locator (p.486) |
getColumnNumber(), getLineNumber(), getPublicId(), getSystemId() |
* DefaultHandler Ŭ·¡½º´Â ContentHandler, DTDHandler, EntityHandler, ErrorHandler ÀÎÅÍÆäÀ̽º¸¦ ¸ðµÎ ±¸ÇöÇÑ Å¬·¡½º (ÀÎÅÍÆäÀ̽º)
import java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class reader1 { public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("... java reader1 filename..."); System.exit(1); } SAXParserFactory factory = SAXParserFactory.newInstance( ); SAXParser parser = factory.newSAXParser( ); myreader handler = new myreader(); parser.parse(new FileInputStream(args[0]), handler); } } public class myreader extends DefaultHandler { public void startDocument( ) { System.out.println("...startDocument"); } public void endDocument( ) { System.out.println("...endDocument"); } public void startElement(String uri, String localName, String qName, Attributes atts) { System.out.print("<"+qName+">"); } public void endElement(String uri, String localName, String qName) { System.out.println("</"+qName+">");} } |
C:>javac
reader1.java C:>java reader1 booklist.xml ...startDocument <list><book><title></title> <author></author> <pages></pages> <price></price> </book> <book><title></title> <author></author> <pages></pages> <price></price> </book> <book><title><subtitle></subtitle> </title> <author></author> <pages></pages> <price></price> </book> ...endDocument |
import
... public class reader2 { public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("... java reader2 filename..."); System.exit(1); } SAXParserFactory ... myreader2 handler = new myreader2(); parser.parse(new FileInputStream(args[0]), handler); } } public class myreader2 extends DefaultHandler { public void startDocument( ) { ... } public void endDocument( ) { ... } public void startElement(String uri, ...) { ... } public void endElement(String uri, String localName, String qName) { System.out.print("</"+qName+">"); } public void characters(char[] chars, int start, int leng) { for (int i=0;i<leng;i++) System.out.print(chars[start+i]); } } |
C:>javac
reader2.java C:>java reader2 booklist.xml ...startDocument <list> <book> <title>XML Bible</title> <author>Gwyneth Paltrow</author> <pages>652</pages> <price>39.99</price> </book> <book> <title>XML Ŭ·¡½º</title> <author>ÀÓ¼ø¹ü</author> <pages>458</pages> <price>19000</price> </book> <book> <title> <subtitle>¿Ç÷°ÀÇ</subtitle> XML By Example</title> <author>È«±æµ¿</author> <pages>529</pages> <price>25000</price> </book> </list>...endDocument |
import ... public class reader3 { public static void main(String[] args) throws Exception { ... myreader3 handler = new myreader3(); ... } } public class myreader3 extends DefaultHandler { private StringBuffer titles = new StringBuffer(); private StringBuffer authors = new StringBuffer(); private boolean isTitle, isAthor = false; private int num=1; public void startElement(String uri, String localName, String qName, Attributes atts) { if (qName.equals("title")) { isTitle=true; titles.setLength(0); } else if (qName.equals("author")) { isAuthor=true; authors.setLength(0); } } public void endElement(String uri, String localName, String qName) { if (qName.equals("book")){ System.out.println("["+num+"] "+ authors.toString()+", "+titles.toString()); num++; } } public void characters(char[] chars, int start, int leng) { if (isTitle) { isTitle=false; titles.append(chars,start,leng); } else if (isAuthor) { isAuthor=false; authors.append(chars,start,leng); } } } |
C:>javac
reader3.java |
|
<book
InStock="8">¨ç ¨è<title>¨é* ¨ê*<subtitle>¨ë¿Ç÷°ÀǨì</subtitle>¨í ¨îXML By Example¨ï</title>¨ð ¨ñ<author>¨ò*È«±æµ¿¨ó*</author>¨ô <pages>529</pages> <price>25000</price> </book>¨õ** |
¨é* isTitle=true ¨ê* isTitle=false ¨ò* isAuthor=true ¨ó* isAuthor=false ¨õ** System.out.println |
... public void endElement(String uri, ...) { if (qName.equals("book")){ System.out.println("[" + num + "] " + authors.toString() + "," + titles.toString()); num++; } else if (qName.equals("title")) isTitle = false; else if (qName.equals("author")) isAuthor = false; } public void characters( ... ) { if (isTitle) titles.append(chars,start,leng); else if (isAuthor) authors.append(chars,start,leng); } |
C:>javac
reader3.java C:>java reader3 booklist.xml [1] Gwyneth Paltrow, XML Bible [2] ÀÓ¼ø¹ü, XML Ŭ·¡½º [3] È«±æµ¿, ¿Ç÷°ÀÇ XML By Example C:> |
|
<book
InStock="8">¨ç ¨è<title>¨é* ¨ê#<subtitle>¨ë¿Ç÷°ÀǨì#</subtitle>¨í ¨î#XML By Example¨ï#</title>¨ð* ¨ñ<author>¨ò*È«±æµ¿¨ó#</author>¨ô* <pages>529</pages> <price>25000</price> </book>¨õ** |
¨è* isTitle=true ¨ê# ¨ì# ¨î# ¨ï# titles.append() ¨ð* isTitle=false ¨ò* isAuthor=true ¨ó# authors.append() ¨ô* isAuthor=false ¨ñ** System.out.println |
... public class myreader4 extends DefaultHandler { private ... titles, authors, isTitle, isAthor, num ... ; private String stock = new String(); public void startElement(String uri, String localName, String qName, Attributes atts) { if (qName.equals("book")) stock = atts.getValue("InStock"); else if (qName.equals("title")) { isTitle=true; titles.setLength(0); } else if (qName.equals("author")) { isAuthor=true; authors.setLength(0); } } public void endElement(String uri, ...) { if (qName.equals("book")){ System.out.println("["+num+"] "+ authors...+"(Àç°í:"+stock+"±Ç)"); num++; } else if (qName.equals("title")) isTitle = false; else if (qName.equals("author")) isAuthor = false; } public void characters(char[] chars, ...) ... } |
C:>javac
reader4.java |
import
java.io.*; import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class conv { public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("...java reader1 filename"); System.exit(1); } SAXParserFactory factory = SAXParserFactory.newInstance( ); SAXParser parser = factory.newSAXParser( ); myconv handler = new myconv( ); parser.parse(new FileInputStream(args[0]), handler); } } public class myconv extends DefaultHandler { private int nspace = 4; // indentation spaces private int indent = 0; // indentation level private boolean inTag=false; 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) |
C:>javac conv.java |
¡¡ |
DOM |
SAX |
Á¢±Ù ¹æ¹ý | Æ®¸® ±â¹Ý | À̺¥Æ® ±â¹Ý |
ÀåÁ¡ | ¹®¼¿¡ ÀÓÀÇ Á¢±Ù | ¸Þ¸ð¸® È¿À², ºü¸¥ ¼Óµµ |
´ÜÁ¡ | ¸Þ¸ð¸® »ç¿ë·®, ¼Óµµ ´À¸®´Ù | ±¸Á¶¿¡ ´ëÇÑ Á¢±Ù °ï¶õ, Àбâ Àü¿ë, ºê¶ó¿ìÀú Áö¿ø ¾ÈµÊ |
Àû¿ëºÐ¾ß | ±¸Á¶Àû Á¢±ÙÀÌ ÇÊ¿äÇÑ °æ¿ì, ¹®¼ Á¤º¸¸¦ ½±°Ô ÆľÇÇÏ°íÀÚ ÇÒ ¶§ |
¹®¼ ÀϺκи¸ ÀÐÀ» ¶§, µ¥ÀÌÅÍ º¯È¯½Ã, À¯È¿¼º ó¸® |