À̺¥Æ® ¹ß»ý ½ÃÁ¡ | À̺¥Æ® Çڵ鷯 |
* <?xml version="1.0" ?>* <list>* <book InStock="2">* <title>*XML Bible*</title>* <author>*Gwyneth Paltrow*</author>* <pages/>** <price currency="usd">*39*</price>* </book>* </list>* * |
0:startDocument() 1:processingInstruction() 2:startElement() 3:startElement() ¼Ó¼ºÀº ¸Å°³º¯¼ö·Î Àü´Þ 4:startElement() 5:characters() 6:endElement() 7:startElement() 8:characters() 9:endElement() 10:startElement() 11:endElement() À̺¥Æ® 2ȸ 12:startElement() 13:characters() 14:endElement() 15:endElement() ¼Ó¼ºÀº ¸Å°³º¯¼ö·Î Àü´Þ 16:endElement() 17:endDocument() |
import javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class MyHandler extends DefaultHandler { public static void main(String[] args) throws Exception { if (args.length < 1) ...¿¡·¯¸Þ½ÃÁö MyHandler myhand = new MyHandler( ); myhand.read(args[0]); } public void read (String fileName) throws Exception { XMLReader myhand = XMLReaderFactory.createXMLReader( "org.apache.xeceres.parsers.SAXParser"); myhand.setContentHandler(this); myhand.parse(fileName); } public void startDocument( ) throws SAXException { ... } public void endDocument( ) throws SAXException { ... } public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { ... } public void endElement(String uri, String localName, String qName) throws SAXException {...} public void characters(char[] chars, int start, int length ) throws SAXException { ... } public void processingInstruction(String target, String data) throws SAXException {...} } |
ÇÊ¿äÇÑ
Ŭ·¡½º Æ÷ÇÔ Çڵ鷯 Ŭ·¡½º Á¤ÀÇ (DefaultHandler) ¸ÞÀÎ event handler »ý¼º ÆÄ½Ì ¸Þ¼Òµå È£Ãâ ÆÄ½Ì ¸Þ¼Òµå ÀÛ¼º parser »ý¼º ¹× ÇÒ´ç event handler ÇÒ´ç parser È£Ãâ/½ÇÇà event Handler |
import ... public class MyHandler extends DefaultHandler { public static void main(String[] args) throws Exception { ... } public void read (String fileName) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance( ); SAXParser parser = factory.newSAXParser( ); parser.parse(filename, this); } public void startDocument( ) throws SAXException { ... } ... } |
Çڵ鷯 Ŭ·¡½º Á¤ÀÇ parser factory »ý¼º parser »ý¼º parser È£Ãâ/½ÇÇà½Ã event handler ÇÒ´ç event Handler |
import
javax.xml.parsers.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class reader1 extends DefaultHandler { public static void main(String[] args) throws Exception { if (args.length < 1) System.out.println("... java reader1 filename..."); reader1 myreader = new reader1( ); myreader.read(args[0]); } public void read(String fileName) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance( ); SAXParser parser = factory.newSAXParser( ); parser.parse(fileName, this); } public void startDocument( ) throws SAXException { System.out.println("...startDocument"); } public void endDocument( ) throws SAXException { System.out.println("...endDocument"); } public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.print("<"+qName+">"); } public void endElement(String uri, String localName, String qName) throws SAXException { 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 extends DefaultHandler { public static void main(String[] args) throws Exception { if (args.length < 1) System.out.println("... java reader2 filename..."); reader2 myreader = new reader2( ); myreader.read(args[0]); } public void read(String fileName) throws Exception public void startDocument( ) throws SAXException public void endDocument( ) throws SAXException public void startElement(String uri, ...) ... public void endElement(String uri, String localName, String qName) throws SAXException { System.out.print("</"+qName+">");} public void characters(char[] chars, int start, int leng) throws SAXException { 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 |
public
class reader3 extends DefaultHandler { private StringBuffer titles = new StringBuffer(); private StringBuffer authors = new StringBuffer(); private boolean isTitle, isAthor = false; private int num=1; public static void main(String[] args) throws E... public void read(String fileName) throws Exception public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 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) throws SAXException { if (qName.equals("book")){ System.out.println("["+num+"] "+ authors.toString()+", "+titles.toString()); num++; } } public void characters(char[] chars, int start, int leng) throws SAXException { if (isTitle) { isTitle=false; titles.append(chars,start,leng); } else if (isAuthor) { isAuthor=false; authors.append(chars,start,leng); } } } |
C:>javac
reader3.java C:>java reader3 booklist.xml [1] Gwyneth Paltrow, XML Bible [2] ÀÓ¼ø¹ü, XML Ŭ·¡½º [3] È«±æµ¿, C:>_ |
...¼öÁ¤... public void endElement(String uri, ...) throws SAXException { if (qName.equals("book")){ System.out.println("[" + num + "] " + authors.toString() + "," + titles.toString()); num++; } else if (isTitle) isTitle = false; else if (isAuthor) isAuthor = false; } public void characters( ... ) throws SAXException { 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] È«±æµ¿, ¿Ç÷°ÀÇ C:>_ |
public
class reader4 extends DefaultHandler { private ... titles, authors, isTitle, isAthor, num ... ; private String stock = new String(); public static void main(String[] args) throws Ex... public void read(String fileName) throws Exception public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 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++; } } public void characters(char[] chars, ...) ... } |
½ÇÇà |
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> =============== |
Parser |
parse(), setDocumentHandler(), setDTDHandler(), setEntityResolver(), setErrorHandler() |
DocumentHandler |
startDocument(), endDocument(), startElement(), endElement(), characters(), ignorableWhitespace(), processingInstruction(), setDocumentLocator() |
DTDHandler |
notationDecl(), unparsedEntityDecl() |
ErrorHandler |
warning(), error(), fatalError() |
Attributes |
getLength(), getQName(i), getType(i), getValue(i) |
Locator |
getColumnNumber(), getLineNumber(), getPublicId(), getSystemId() |
¡¡ |
DOM |
SAX |
Á¢±Ù ¹æ¹ý | Æ®¸® ±â¹Ý | À̺¥Æ® ±â¹Ý |
ÀåÁ¡ | ¹®¼¿¡ ÀÓÀÇ Á¢±Ù | ¸Þ¸ð¸® È¿À², ºü¸¥ ¼Óµµ |
´ÜÁ¡ | ¸Þ¸ð¸® »ç¿ë·®, ¼Óµµ ´À¸®´Ù | ±¸Á¶¿¡ ´ëÇÑ Á¢±Ù °ï¶õ, Àбâ Àü¿ë, ºê¶ó¿ìÀú Áö¿ø ¾ÈµÊ |
Àû¿ëºÐ¾ß | ±¸Á¶Àû Á¢±ÙÀÌ ÇÊ¿äÇÑ °æ¿ì, ¹®¼ Á¤º¸¸¦ ½±°Ô ÆľÇÇÏ°íÀÚ ÇÒ ¶§ |
¹®¼ ÀϺκи¸ ÀÐÀ» ¶§, µ¥ÀÌÅÍ º¯È¯½Ã, À¯È¿¼º ó¸® |