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 JDOM & JDOM Programming with Java
 
À̹ø±Û¿¡¼­´Â SAX¿Í DOMÀÇ ÀÚ¹Ùº¯ÇüÆÇÀÎ JDOM¿¡ ´ëÇÑ °³¿ä¿Í ÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ. ( 2003/03/17 ) 317
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

À̹ø±Û¿¡¼­´Â SAX¿Í DOMÀÇ ÀÚ¹Ùº¯ÇüÆÇÀÎ JDOM¿¡ ´ëÇÑ °³¿ä¿Í ÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿©
¾Ë¾Æº¸µµ·Ï ÇÏÀÚ.

¢º About JDOM

    - Java-Centric, Object Oriented
    - °¢ Æļ­ÀÇ ±¸Çö¹æ½ÄÀÌ ´Ù¸¥µ¥¼­ ³ªÅ¸³ª´Â ºñÀÏ°ü¼ºÀÇ ¹®Á¦ ÇØ°áÁ¦½Ã
    - SAX, DOM°úÀÇ Á¶È­
    - Loosely coupled XML
    - License Free, Open Source


Jason Hunter¿Í Duncan Davidson°ú ÇÔ²² µðÀÚÀÎÇÏ¿© ¸¸µé¾úÀ¸¸ç ±×µéÀÇ À¥»çÀÌÆ®(www.jdom.org)¿¡´Â
Á÷°üÀûÀÌ°í, º¹ÀâÇÑ XMLÀ» Á¶ÀÛÇÏ´Â °ÍÀÌ ¾Æ´Ï¶ó, Java API¸¦ »ç¿ëÇÏ´Â °Íó·³ °£´ÜÇÏ°í, 
ÀÚ¹ÙÁß½ÉÀûÀ̸ç, ÀÚ¹ÙÃÖÀûÈ­(java optimized)¸¦ ÁöÇâÇÏ°í ÀÖ´Ù.

SAX´Â À̺¥Æ®±â¹ÝÀÇ documentÆĽ̰ú 󸮸¦ ÇÒ ¼ö ÀÖ´Â ºü¸£°í °­·ÂÇÑ ±â´ÉÀ» Á¦°øÇÏ´Â 
APIÀιݸé, DOMÀº ¸Å¿ì À¯¿¬ÇÑ ±¸Á¶¸¦ °¡Áö°í ÀÖÁö¸¸ ´ÊÀº 󸮽ð£°ú documentó¸®ÀýÂ÷°¡ 
º¹ÀâÇÏ´Ù´Â ³­Á¡À» °¡Áö°í Àִµ¥, JDOMÀº ÀÌ·¯ÇÑ SAXÀÇ ºü¸¥ ¼öÇà¼Óµµ¿Í DOMÀÇ ³ôÀº 
À¯¿¬¼º µîÀÇ ÀåÁ¡À» ÀûÀýÈ÷ È¥ÇÕÇØ ³õÀº ÇüŶó ºÁµµ ¹«¹æÇÏ´Ù. 

Loosely coupled¶õ ¸»Àº SAX³ª DOM¿¡ ±â¹ÝÇÏ¿© ¸ðµ¨¸µ µÇÁö ¾Ê¾Ò±â ¶§¹®¿¡ XML°ú´Â 
´À½¼ÇÑ ¿¬°áÀ̶ó ÇÒ ¼ö ÀÖ´Ù.

DOMÀÇ °æ¿ì XML documentÀÇ Ã³¸®¿¡ À־ ³»¿ëÀ» ¸ðµÎ ¸Þ¸ð¸®»ó¿¡ ¿Ã·Á³õÀº ÈÄ Á¶ÀÛÇÏ°Ô 
µÇ¹Ç·Î ½ÇÁ¦ Å« ¿ë·®ÀÇ XML documentÀÇ Ã³¸®½Ã ·Îµù½Ã°£, °Ë»ö, »èÁ¦ µîÀÇ ½Ã°£ÀÌ ´Ù·® ¼Ò¸ðµÈ´Ù´Â 
´ÜÁ¡À» °¡Áö°í ÀÖ¾ú´Ù. 

ÀÌ¿¡ SAXÀÇ µîÀåÀ» ³º°Ô µÇ¾ú´Âµ¥ SAX´Â µ¥ÀÌÅ͸¦ Á¢±ÙÇÏ´Â ¹æ½ÄÀÌ °´Ã¼ÁöÇâÀûÀÎ ¹æ¹ýÀÌ 
¾Æ´Ï¶ó  À̺¥Æ® Áß½ÉÀûÀÎ ¼º°ÝÀ¸·Î ¿Å°Ü°¡¾ß ÇÑ´Ù´Â °ÍÀ̾ú´Ù.

 JDOMÀº SAX¿Í DOMÀÇ ´ÜÁ¡ÀÎ À̺¥Æ® Áß½ÉÀûÀÌ°í, document¿¡ ´ëÇÑ ¸Þ¸ð¸® ·Îµå°¡ ¾Æ´Ï¶ó,
 ÀÚ¹ÙÀÇ °´Ã¼ÁöÇâÀûÀ̸ç, ¸Þ¸ð¸®»óÀÇ ·Îµå¸¦ ¿ä±¸ÇÏÁöµµ ¾Ê´Â´Ù´Â Á÷°üÀû 
 document object modelÀ» Á¦°øÇϵµ·Ï ÇÑ´Ù.

¢º JDOM API

    org.jdom
    org.jdom.input
    org.jdom.output
    org.jdom.adapter
    org.jdom.filter 
    org.jdom.transform 

org.jdom : Attribute, CDATA, Namespace, Document, CommentµîµîÀÇ ÁÖ¿äÇÙ½ÉŬ·¡½ºµé·Î ±¸¼ºµÇ¾î ÀÖ´Ù.

org.jdom.input : JDOMFactoryÀÎÅÍÆäÀ̽º°¡ Á¸ÀçÇϸç XML¼Ò½º·ÎºÎÅÍ DOM, SAXÇüÅÂÀÇ Document°´Ã¼¸¦ 
»ý¼ºÇϴµ¥ »ç¿ëµÈ´Ù.

org.jdom.output : Ãâ·Â¿¡ °ü·ÃµÈ Ŭ·¡½º°¡ Á¸ÀçÇϸç DOM,SAX API¿Í adaptingÇÏ¿© XMLÀ» Ãâ·ÂÇØÁÖ´Â 
DOM,SAXÀÇ Outputter¿Í JDOMÀÚüÀÇ XML·Î Ãâ·Â½ÃÄÑÁÙ ¼ö ÀÖ´Â XMLOutputterŬ·¡½º°¡ Á¸ÀçÇÑ´Ù.

org.jdom.adapter : ÇöÀç »ç¿ëµÉ ¼ö ÀÖ´Â DOM¿¡ ´ëÇÑ °øÅëÀÎÅÍÆäÀ̽º¸¦ Á¦°øÇÑ´Ù.(APIÂüÁ¶)

org.jdom.filter : JDOM object¿¡ ´ëÇÑ filteringÀ» ¼öÇàÇÑ´Ù.

org.jdom.transform : JDOM documentÀÇ tree¿Í source¿¡ ´ëÇÑ Çڵ鸵À» ´ã´çÇÑ´Ù

JDOMÀº DOM°ú SAX¿¡ ´ëÇÑ Çڵ鸵À» ¸ðµÎ ó¸®ÇÒ ¼ö ÀÖ´Ù. ´ÙÀ½¿¡ ³ª¿À´Â °£´ÜÇÑ JDOMÇÁ·Î±×·¥À» 
ÀÛ¼ºÇØ º½À¸·Î¼­ JDOMÀÇ »ç¿ë¹æ¹ýÀ» ÀÍÇôº¸µµ·Ï ÇÏÀÚ

JDOMÀ» ÀÌ¿ëÇÏ¿© SAX¹æ½ÄÀÇ Ã³¸®¸¦ ¾î¶»°Ô ÇÏ´ÂÁö¿Í java api¿ÍÀÇ ¿¬°è°¡ ¾ó¸¶³ª ½¬¿öÁú ¼ö 
ÀÖ´Â Áö¸¦ ¾Ë¼ö Àִµ¥,  SAXÀÇ ºü¸¥ ¼Óµµ¿Í DOMÀÇ À¯¿¬¼ºÀִ Ư¡À» ¸ðµÎ °¡Áú ¼ö ÀÖ´Â 
°£´ÜÇÑ ÄÚµå·Î JDOMÀ¸·Î ½Çü¸¦ º¸¿©º»´Ù.

¸ÕÀú ÇÁ·Î±×·¥À» ÀÛ¼ºÇÏ°í ½ÇÇàÇÏ·Á¸é JDOM¿¡ ´ëÇÑ ÆÐÅ°Áö°¡ ÇÊ¿äÇÔÀº ¹°·ÐÀÌ¸ç ¾Æ·¡ÀÇ »çÀÌÆ®¿¡¼­
´Ù¿î¹Þµµ·Ï ÇÏÀÚ.

JDOM site : http://www.jdom.org/downloads/index.html

À§ÀÇ »çÀÌÆ®¿¡¼­ JDOM¿¡ ´ëÇÑ ´Ù¿î·Îµå¸¦ ¹Þ°í Àü¿¡ SAX³ª DOMó·³ ÇØ´ç ÆÐÅ°Áö°¡ ÀÖ´Â °÷¿¡
¾ÐÃàÀ» Ç®¾î³Öµµ·Ï ÇÏÀÚ.

¾Æ·¡ÀÇ ÄÚµùÀ» ÇÑ´Ù.

¢ºJDOMÀ» ÀÌ¿ëÇÑ °£´ÜÇÑ HelloWorldÇÁ·Î±×·¥

9:     import org.jdom.Element;
10:   import org.jdom.Document;
11:   import org.jdom.output.XMLOutputter;
21:   public class JDOMHelloWorld {
22:   	public static void main(String [] args) {
24:   		Element root = new Element("MultiCampus");
27:   		root.setText("HelloWorld!");
30:   		Document doc = new Document(root);
32:   		try {
33:   			XMLOutputter out = new XMLOutputter();
35:   			out.output(doc, System.out);
36:   		}catch(Exception e) {
37:   			e.printStackTrace();
38:   		}
39:   	}
40:   }


Line 9~11 : ÀϹÝÀûÀÎ DOM, SAXÀÇ API¿Í ¸¶Âù°¡Áö·Î jdomÀÇ ÆÐÅ°Áö Ŭ·¡½º¸¦ importÇÏ´Â µ¥ 
9¶óÀÎÀÇ Element´Â xmlÀÇ element¿Í, document ¶ÇÇÑ xmlÀÇ document¿Í ¸ÅÇεǸé jdomÀÇ Ãâ·ÂÀ» 
Áö¿øÇÒ ¼ö ÀÖ´Â XMLOutputterŬ·¡½º¸¦ Á¤ÀÇ

Line 24 : Multicampus¶ó´Â XML Element¸¦ »õ·Ó°Ô »ý¼ºÇÑ´Ù.
Line 27 : ÇØ´ç element¿¡ HelloWorld¶ó´Â °ªÀ» ºÎ¿©ÇÑ´Ù.
Line 33 : Ãâ·ÂÀ» ´ã´çÇÑ XMLOutputterÀÇ °´Ã¼¸¦ »ý¼ºÇÑ´Ù.
Line 35 : ÇöÀç °¡Áö°í ÀÖ´Â document¸¦ defaultÃâ·ÂÀåÄ¡ÀÎ screenÀ¸·Î Ãâ·ÂÇÑ´Ù

¢º SAX¸¦ ÀÌ¿ëÇÑ JDOM Program 

1:   SAXBuilder builder = new SAXBuilder();
2:   Document doc = builder.build(new File(args[0]));
3:   DocType docType = doc.getDocType();
4:   Element root = doc.getRootElement();
5:   trace("Input File : " + args[0]);
6:   if( docType == null ) {
7: 	    trace("DocType is null !!");
8:   } else {
9:	    trace("getElementName() : " + docType.getElementName());
10:	    trace("getPublicID() : " + docType.getPublicID() );
11:	    trace("getSystemID() : " + docType.getSystemID() );
12:	    trace(docType.toString());
13:  }
14:  trace("root.getName() : " + root.getName());
15:  List children = root.getChildren();
16:  Iterator iter = children.iterator();
17: while( iter.hasNext() ) {
18: 	trace(iter.next().toString());
19: }


Line 1 : SAXÆļ­·Î¼­ ó¸®ÇÒ ¼ö ÀÖµµ·Ï SAXBuilderÀÇ °´Ã¼¸¦ »ý¼ºÇÑ´Ù.
Line 2 : SAX Builder¸¦ ÀÌ¿ëÇÏ¿© ÀԷ¹ÞÀº ÆÄÀÏÀ» ÆĽÌÇÑ´Ù.
Line 3 : ÆÄ½ÌµÈ documentÀÇ document typeÀ» ¾ò¾î³½´Ù.
Line 4 : ÆÄ½ÌµÈ documentÀÇ root element¸¦ ¾ò¾î³½´Ù. 
Line 9~12 : DocTypeÀÌ ÀÖ´Ù¸é ÇØ´ç ¿ä¼ÒÀÇ ID¸¦ Âï´Â´Ù.
Line 14 :  rootÀÇ À̸§À» Âï´Â´Ù.
Line 15~18 : root  elementÀÇ ÇÏÀ§³ëµåµéÀº CollectionŸÀÔÀ¸·Î º¯ÇüÇÑ ÈÄ child¸¦ Âï¾îº¸µµ·Ï ÇÑ´Ù.

¢ºDOMÀ» ÀÌ¿ëÇÑ JDOMÇÁ·Î±×·¥

DOM default parser¸¦ ÀÌ¿ëÇÏ¿© DOMBuilderÀÇ °´Ã¼¸¦ »ý¼ºÇϸç, SAXÀÇ building¹æ½Ä°ú µ¿ÀÏÇÏ´Ù.
À§ÀÇ ¹æ½Ä¿¡¼­´Â DOMBuilderÀÇ °´Ã¼¸¦ »ý¼ºÇÏ¿© ÆÄÀÏÀ» ÀоîµéÀÌ°í ÆĽÌÇÏ°Ô µÈ´Ù.
¶Ç ´Ù¸¥ ¹æ¹ýÀº DOMÀÇ ÆÄÀÏÀ» ÀоîµéÀÌ´Â ¹æ½ÄÀº SAXº¸´Ù ÈξÀ ´À¸®¹Ç·Î SAXÀÇ ÆÄÀÏÀ» 
ÀоîµéÀÌ´Â ¹æ½ÄÀ» ÀÌ¿ëÇÏ¿© JDOM document°´Ã¼·Î ¸¸µç ´ÙÀ½ DOMÀ¸·Î º¯È¯ÇÏ´Â
¹æ½ÄÀ» ÃëÇÒ ¼öµµ ÀÖÀ» °ÍÀÌ´Ù

/*
 * @(#)DOMBuilderExample.java	1.00 2002.08.05
 *
 * 
 */

import java.io.File;
import java.util.List;
import java.util.Iterator;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
/**
 *	JDOMÀ» ÀÌ¿ëÇÑ Simple DOMBuilder Class
 *	
 * 

* * @author Choi Ji Woong(carouser@dreamwiz.com) * @version 1.00, 2002.05.04 * @since Web F/W 1.0 */ public class DOMBuilderExample { public static void main(String [] args) { if( args.length != 1 ) showUsage(); SAXBuilder builder = new SAXBuilder(); try{ Document doc = builder.build(new File(args[0])); DocType docType = doc.getDocType(); Element root = doc.getRootElement(); trace("Input File : " + args[0]); if( docType == null ) { trace("DocType is null !!"); }else { trace("getElementName() : " + docType.getElementName()); trace("getPublicID() : " + docType.getPublicID() ); trace("getSystemID() : " + docType.getSystemID() ); trace(docType.toString()); trace("=============================================="); trace("root.getName() : " + root.getName()); List children = root.getChildren(); Iterator iter = children.iterator(); while( iter.hasNext() ) { Object obj = iter.next(); trace(obj.toString()); trace("[Class Name] " + obj.getClass().getName()); } } }catch(Exception e) { e.printStackTrace(); } } public static void showUsage() { System.out.println("Usage : run DOMBuilderExample "); System.exit(1); } public static void trace(String data) { System.out.println("[ DOMBuilderExample ] " + data); } } À§ÀÇ ÇÁ·Î±×·¥Àº ¾Õ¼­ º¸¾Ò´ø SAXBuilder¸¦ ÀÌ¿ëÇÑ JDOMÇÁ·Î±×·¥°ú À¯»çÇÏ´Ù ´Ù¸¥Á¡À̶ó°í´Â SAX¿ÍÀÇ Â÷ÀÌó·³ ¸Þ¸ð¸®»ó¿¡ ÇØ´ç XML Document¿¡ ´ëÇÑ ³»¿ëÀ» ÀÌ¿ëÇÏ¿© Á¶ÀÛÀ» ÇÑ´Ù´Â °ÍÀÌ´Ù. ƯÀÌÇÑ Á¡Àº ÀÚ¹ÙÁöÇâÀûÀ̶ó°í Çß´ø ¸¸Å­ ¾Æ·¡ÀÇ Äڵ带 À¯½ÉÈ÷ º¼ Çʿ伺ÀÌ ÀÖ´Ù. List children = root.getChildren(); Iterator iter = children.iterator(); º¸Åë DOM¿¡¼­´Â NodeList¸¦ ÅëÇÏ¿© DOM tree¸¦ ¼øȸÇÏ°Ô ÇÏ°í ÀÖÁö¸¸ JDOM¿¡¼­´Â ¹Ù·Î getChildren()¸Þ¼Òµå¸¦ ÅëÇÏ¿© ÀÚ¹ÙÂÊ ¿ÀºêÁ§Æ®ÀÎ ListÀÇ °´Ã¼¸¦ ¾ò¾î³»¾î ¹Ù·Î traverseÇÒ ¼öÀÖ´Â Á¶°ÇÀ» ¸¶·ÃÇØ ³õ°í ÀÖ´Ù. XMLÀÇ ±¸Á¶¿¡ ´ëÇÑ ¾î´ÀÁ¤µµÀÇ ÀÌÇظ¸ ÀÖ´Ù¸é ÀÚ¹Ù¿¡ ´ëÇÑ Áö½Ä¸¸À¸·Îµµ ÃæºÐÈ÷ ÄÚµùÀ» Çس¾ ¼ö ÀÖÀ» Á¤µµ·Î ½¬¿î °ÍÀÌ ¹Ù·Î JDOMÀÌ´Ù. À̹ø Æí¿¡¼­´Â XMLÀÇ ÀÚ¹ÙÁöÇâÀûÀÎ JDOM¿¡ ´ëÇÏ¿© »ìÆ캸¾Ò´Ù.

 
1
References
 
JDOM site : http://www.jdom.org/downloads/index.html
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD