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 DOM & DOM Programming with Java
 
DOMÀÌ ¹«¾ùÀÎÁö¿¡ ´ëÇÏ¿© °£·«ÇÑ °³¿ä¸¦ ¸ÕÀú º¸µµ·Ï ÇÏ°Ú°í, DOM Level 1, 2, 3ÀÇ °³·«ÀûÀÎ ³»¿ë°ú DOMÀ» ÀÌ¿ëÇÑ ÀÚ¹ÙÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸°íÀÚ ÇÑ´Ù. ( 2003/03/13 ) 419
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

À̹ø±Û¿¡¼­´Â DOM(Document Object Model)¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸µµ·Ï ÇÏÀÚ. 
DOMÀÌ ¹«¾ùÀÎÁö¿¡ ´ëÇÏ¿© °£·«ÇÑ °³¿ä¸¦ ¸ÕÀú º¸µµ·Ï ÇÏ°Ú°í, DOM Level 1, 2, 3ÀÇ °³·«ÀûÀÎ
³»¿ë°ú DOMÀ» ÀÌ¿ëÇÑ ÀÚ¹ÙÇÁ·Î±×·¡¹Ö ¹æ¹ý¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸°íÀÚ ÇÑ´Ù.

¢º About DOM

    - W3C¿¡¼­ Á¦¾È
    - Level 1 Recommendation : 1998.10
    - Level 2 Recommendation : 2000.11
    - Level 3 Working Draft : 2002.1
    - XML DocumentÀÇ Á¶ÀÛ ¹æ¹ý Á¦°ø
    - Java Script¿¡ À̽ļºÀ» ºÎ¿©ÇÏ·Á´Â ¸ñÀû¿¡¼­ Á¦¾È

DOMÀº document¸¦ ±¸¼ºÇÏ°í ÀÖ´Â ºÎºÐµéÀ» Á¶ÀÛÇØÁÙ ¼ö ÀÖµµ·Ï Çϴµ¥  ÀÌ ¸»À» ´Þ¸®Çϸé DOMÀ» 
ÀÌ¿ëÇÏ°Ô µÉ ¶§ documentÀÇ ¿ä¼Ò¸¦ Ç¥ÇöÇÏ´Â °´Ã¼µé¿¡ ´ëÇÑ Á¶ÀÛÀ¸·Î¼­ °¢°¢ÀÇ ¼½¼Ç»çÀ̸¦ ¿Å°Ü´Ù´Ò ¼ö 
ÀÖ´Ù. ÀÌ´Â navigation or manipulate¶ó´Â ¿ë¾î·Î ´ëº¯ÀÌ µÉ ¼ö ÀÖÀ¸¸ç, document¸¦ °´Ã¼¾ç½ÄÀ¸·Î 
Ç¥ÇöÇÑ ¸ðµ¨À̶ó ÇÒ ¼ö ÀÖ´Ù.

¶ÇÇÑ Javascript(javascript´Â µ¿ÀûÀ̸ç À¥ºê¶ó¿ìÁ® Á¾·ù¿¡ °ü°è¾øÀÌ µ¿ÀÏÇÏ°Ô »ç¿ëÇÒ ¼ö ÀÖ´Ù)ÀÇ 
À̽ļººÎ¿©¸ñÀû¿¡¼­ Á¦¾ÈµÇ¾úÀ¸¸ç,  DOMÀÇ ½ÃÃÊ´Â µ¿ÀûÀÎ HTMLÀÇ ÇѺκÐÀ¸·Î¼­ ½ÃÀ۵Ǿú´Ù°í 
ÇÒ ¼ö Àִµ¥ DOMÀº HTML¿ä¼ÒµéÀÇ »ý¼º, °»½Å, »èÁ¦¸¦ À§ÇØ »ç¿ëµÇ´Â 
ÀÚ½ÅÀÇ °´Ã¼µé°ú HTML»çÀÌÀÇ ¸ÅÇÎÀ» Á¦°øÇØ ÁÙ ¼ö ÀÖµµ·Ï ÇÑ´Ù.

DOMÀÇ Áß¿äÇÑ ¸ñÀûÀº ±¤¹üÀ§ÇÑ È¯°æ°ú ÀÀ¿ëÇÁ·Î±×·¥¿¡¼­ Ç¥ÁØÀûÀÎ ÇÁ·Î±×·¡¹Ö ÀÎÅÍÆäÀ̽º¸¦ 
Á¦°øÇϱâ À§ÇÑ °ÍÀ¸·Î °³¹ßÀÚ´Â ¹®¼­ÀÇ »ý¼º,¹®¼­±¸Á¶ÀÇ Å½»ö,³»¿ëÀÇ Ãß°¡,»èÁ¦,¼öÁ¤À» APIÀÎ DOMÀ»
ÅëÇØ ¼öÇà ÇÒ ¼ö ÀÖ´Ù.W3C´Â ÀÌ·¯ÇÑ ±â´ÉÀ» ÇÁ·Î±×·¥ ¾ð¾î Á߸³ÀûÀ¸·Î ¸¸µé±â À§ÇØ 
OMG(Object management Group)ÀÇ IDL(Interface Definition Language)¸¦ »ç¿ëÇÏ¿©
DOM API ¸í¼¼¸¦ ¸¸µé¾ú´Ù.

¢º DOMÀÇ Æ¯Â¡

- DOM »ç¾çÀº XML°ú HTML À» À§ÇÑ programmatic interface¸¦ Á¤ÀÇÇÑ´Ù.
- DOMÀº ³í¸®Àû ¹®¼­±¸Á¶¸¦ ÅëÇØ ÇÁ·Î±×·¥ÀÌ ¹®¼­¿¡ Á¢±ÙÇÏ´Â ¹æ½ÄÀ» Á¤ÀÇ Çϸç, °´Ã¼ÁöÇâ ¸ðµ¨ÀÇ
'°´Ã¼¸ðµ¨' À» µû¼­ Áö¾îÁø À̸§ÀÌ´Ù.
- DOMÀ» »ç¿ëÇÏ¿© ÀÀ¿ëÇÁ·Î±×·¥ °³¹ßÀÚ´Â ÄÄÇ»ÅÍ ±âÁ¾°ú ÇÁ·Î±×·¥¹Ö ¾ð¾îÀÇ Á¾·ù¿¡ °ü°è¾øÀÌ ¹®¼­¸¦ 
»ý¼ºÇÏ°í ¹®¼­ÀÇ ³»¿ë°ú ±¸Á¶¸¦ µ¿ÀûÀ¸·Î Á¢±ÙÇÏ°í Á¶ÀÛÇÒ ¼ö ÀÖ´Ù

¢º DOM Level 1

    - 97³âÈÄ¹Ý ½ÃÀÛ 98³â ±Ç°í¾È¹ßÇ¥
    - Document class¿Í Method¸¸À» Á¦°ø
    - http://www.w3c.org/tr/red-dom-level-1
    - DOM serialization, persistence±â´ÉÁ¦°ø ¾ÈÇÔ
    - 2000³â 11¿ù DOM Level 1 Second Ed.¹ßÇ¥


W3CÀÇ ±Ç°í¾È(recommendation:Ç¥ÁØÀÌ ¾Æ´Ï¶ó Ç¥ÁØ¿¡ ÁØÇÏ´Â ¸íĪÀ» Ç¥ÇöÇÏ´Â ¿ë¾î)À¸·Î¼­ ÃÖ±Ù 
¹ßÇ¥½Ã±â´Â 1998³â 10¿ù 1ÀÏÀÌ´Ù.
½ÇÁ¦ ±âº»ÀûÀΠŬ·¡½º¸¸À» Á¦°øÇßÀ¸¸ç XMLÆÄÀÏÀ» ÀÐ¾î ¸Þ¸ð¸®·Î ·ÎµåÇϰųª DOMÀ» ±¸¼ºÇÏ´Â 
µ¥ÀÌÅ͵éÀ» Àоîµé¿© ÆÄÀÏ·Î ÀúÀåÇÏ´Â ¸Þ¼ÒµåµéÀº Á¦°øÀÌ µÇÁö ¾Ê¾Ò´Ù.

2000³â 11¿ù Vidur Apparao, Steve Byrne, Mike Champion, Scott Isaacs, Ian Jacobs, Arnaud Le Hors, 
Gavin Nicol, Jonathan Robie, Robert Sutor, Chris Wilson, Lauren Wood ¿¡ ÀÇÇÏ¿© Second EditionÀ» 
¹ßÇ¥ÇÏ°Ô µÈ´Ù.

¢º DOM Level 2

    - 2000³â 11¿ù W3C ±Ç°í¾ÈÀ¸·Î È®Á¤
    - Level 1ÀÇ ´ëü°¡ ¾Æ´Ñ ±â´ÉÃß°¡ÀÇ ÇüÅÂ
    - ±âÁ¸ÀÇ CORBA2.2¿¡¼­ CORBA2.3.1 IDL Ãß°¡
    - XML Namespaceó¸®ÀÇ Ãß°¡
    - XML, HTML, StyleSheet, CSSµîÀÇ Ã³¸®¿¡ ´ëÇÑ ±¸Ã¼Àû ÀÎÅÍÆäÀ̽º Á¦°ø

DOM Level2´Â ±âÁ¸ÀÇ Level1ÀÌ ±¸½ÄÀÌ µÇ°Ô²û ¿ÏÀüÈ÷ ´ëüÇÏ´Â °ÍÀÌ ¾Æ´Ñ ¸î°¡Áö ±â´ÉÀÇ Ãß°¡·Î  
±âÁ¸ÀÇ Level1¿¡¼­ »ç¿ëµÇ¾ú´ø ³»¿ë¿¡ µ¡ºÙÀÎ ÇüÅ·Π±Ç°í¾ÈÀ» ¹ßÇ¥Çß´Ù.
Level1¿¡¼­ Á¦°øµÇ´Â CORBA2.2ÀÇ ¸ÅÇÎÀ» CORBA2.3.1ÀÇ IDLÃß°¡¿Í XML Namespace¿¡ ´ëÇÑ Ã³¸®±â´ÉÀÌ 
Ãß°¡µÇ¾ú°í, Core, Views, Styles, Events, Traversal and RangeÀÇ ±Ç°í¾ÈÀ» ÅëÇÏ¿© Document¿¡ ´ëÇÑ 
±¸Ã¼ÀûÀÎ ÀÎÅÍÆäÀ̽º¸¦ Á¦½ÃÇÏ°í ÀÖ´Ù.

¢º DOM Level 3

    - 2002³â 4¿ù Level3 ÃÖÁ¾ working draft ¹ßÇ¥
    - Views and Formatting, Abstract SchemasÀÇ Level 3Á¦¿Ü
    - Load and Save, Validation, Event, XPath 
    - DOM Level 2¸¦ ±âº»À¸·Î ¼³°è

2002³â 4¿ù ÃÖÁ¾ draft°¡ ¹ßÇ¥µÇ¾úÀ¸¸ç, ±âÁ¸¿¡ ¾ø¾ú´ø load and save±â´ÉÀÇ Ãß°¡°¡ °­·ÂÇÏ´Ù°í 
ÇÒ ¼ö Àִµ¥, DOMInputStream°ú DOMOutpuStreamÀ» ÀÌ¿ëÇÏ¿© writeÇÒ ¼ö Àִ Ư¡À» °¡Áö°í ÀÖ´Ù.
¶ÇÇÑ DOM Xpath: XPath1.0À» »ç¿ëÇÏ¿© DOM Tree¿¡ ÁúÀÇÇÒ ¼ö ÀÖ´Â ±â´ÉÀ» Á¦°øÇÏ°í,
Validation(DocumentÀÇ DTD¿¡ ´ëÇÑ validationüũ)À» ÇÒ ¼ö ÀÖ´Â ±â´ÉÀ» Á¦°øÇÑ´Ù.
Event ¸ðµ¨ : Level 2ÀÇ Events ¸ðµ¨°ú °ÅÀÇ Èí»çÇÏ¸ç ¸¶¿ì½º, Å°º¸µåµî°ú °°Àº À̺¥Æ® ÁöÇâÀûÀÎ
XMLÆ®¸®¸¦ Æ÷ÇÔÇÑ´Ù.

¢º XMLÀÇ DOM TreeÇ¥Çö

¾Æ·¡¿Í °°Àº XML µµÅ¥¸ÕÆ®°¡ ÀÖ´Ù°í °¡Á¤ÇÏÀÚ.

<?xml version="1.0">
<address-book>
    <person>
        <name>Choi ji woong</name>
        <address>Seoul</address>
        <phone>011-xx</phone>
    </person>
    <person>
        <name>jeon hong seong</name>
        <address>Suji</address>
        <phone>016-xx</phone>
    </person>
</address-book>

À§ÀÇ ³»¿ëÀ» ½ÇÁ¦ ¿ì¸®°¡ º¼¼ö ÀÖ´Â ³í¸®ÀûÀÎ °´Ã¼¸ðµ¨·Î Ç¥ÇöÇÑ´Ù¸é ¾Æ·¡¿Í °°Àº ±×¸²ÀÌ µÈ´Ù.



À§¿¡¼­ º¸´Â °Íó·³ °¢°¢ÀÇ ¿ä¼ÒµéÀÌ Æ®¸®ÀÇ ±¸Á¶·Î Ç¥ÇöµÉ ¼ö ÀÖÀ¸¸ç, ³»°¡ ¿øÇÏ´Â ¿ä¼Ò¿¡ ´ëÇÏ¿©
Æ®¸®°£ÀÇ À̵¿À» ÅëÇØ Ã£À» ¼ö Àֱ⶧¹®¿¡ °Ë»ö, ¼öÁ¤, »èÁ¦°¡ ¿ëÀÌÇÏ°Ô µÈ´Ù.
DOMÀº ÀÌ¿Í °°ÀÌ ¿øÇÏ´Â ´ë·Î ¹®¼­¿¡ Á¢±ÙÇÒ ¼ö ÀÖµµ·Ï ÇØÁÖ´Â ¿ÀºêÁ§Æ®·Î¼­ XMLÀ̳ª HTML¹®¼­¿¡ 
´ëÇÑ API¸¦ Á¦°øÇÏ°í ÀÖ´Ù.

¢º DOMÀÇ ÀÛµ¿¹æ½Ä



DOM Àº XMLÆļ­¿Í ¾ÖÇø®ÄÉÀÌ¼Ç »çÀÌÀÇ °èÃþ¿¡ ³õ¿©Áø´Ù.
DOMÀº ±×¸²¿¡¼­ º¼ ¼ö ÀÖµíÀÌ SAX(Simple API for XML) Æļ­¿¡¼­ ¾ò¾îÁø °á°ú¸¦ ÀÌ¿ëÇÏ¿©
¹®¼­ °´Ã¼ ¸ðµ¨ÀÎ DOMÀ» ¸¸µé¾î ³½´Ù.

DOM »ý¼º°úÁ¤À» »ìÆ캸¸é..
¸ÕÀú, Æļ­´Â DOM ¿¡¼­ Á¦°øÇÏ´Â DocumentBuilder ¸¦ ÅëÇØ ¿øº» XML Document ¸¦ ÆĽÌÇϱâ À§ÇØ
Document Handler¸¦ »ý¼ºÇÏ°í,  Æļ­´Â SAX¿¡¼­ Á¦°øÇÏ´Â Document Handler¸¦ ÅëÇØ ÀÔ·ÂµÈ ¿øº» 
XML ¹®¼­¸¦ ÆĽÌÇÏ¿© Document °´Ã¼¸¦ »ý¼º½ÃÅ°°í ¿¤¸®¸ÕÆ®¿Í ¹®ÀÚ¿­ÀÌ µé¾î¿Ã ¶§¸¶´Ù DOMÀÚ·á 
±¸Á¶¸¦ Â÷·Ê·Î ¸¸µé¾î ³ª°£´Ù. 

DOMÀ» ÀÌ¿ëÇÏ¿© XML µµÅ¥¸ÕÆ®¿¡ Á¢±Ù ÇÒ ¼ö ÀÖ´Â ¾ÖÇø®ÄÉÀ̼ÇÀ» ¸¸µé±â À§Çؼ­´Â XMLÆļ­°¡ 
¼³Ä¡µÇ¾î ÀÖ¾î¾ß ÇÏ¸ç ¾Æ·¡ÀÇ »çÀÌÆ®¿¡¼­ ´Ù¿î¹ÞÀ» ¼ö ÀÖ´Ù.

Xerces : http://xml.apache.org/xerces2-j/index.html
Sun XML : http://java.sun.com/xml

¢º DOMÀÇ Interface



DOM ±¸Çö¿¡´Â ¿©·¯ Çü½ÄÀÌ Á¸ÀçÇÑ´Ù. ±âº» Document ÀÎÅÍÆäÀ̽ºÀÎ DOM Core¿Í HTML ¸ðµâÀÎ DOM HTML, 
CSS ¸ðµâÀÎ DOM CSSµîÀÌ ÀÖÀ¸¸ç DOM Core ÀÎÅÍÆäÀ̽º´Â ±âÃÊ ÀÎÅÍÆäÀ̽º(Fundamental Interfaces)¿Í 
È®Àå ÀÎÅÍÆäÀ̽º(Extended Interfaces)·Î ³ª´µ¾îÁú¼ö ÀÖ°í, DOM±¸Çö½Ã ÀڽŸ¸ÀÇ ¼Ó¼º°ú ¸Þ¼Òµå¸¦ Ãß°¡ÇÏ¿© 
ÀÎÅÍÆäÀ̽º¸¦ È®ÀåÇÒ ¼ö ÀÖ´Ù.

ÀÌ¿¡ ´ëÇÑ API´Â ¸ðµÎ Æļ­¿¡ ´ëÇÑ document API¸¦ ¿©·¯ºÐµéÀÌ ÂüÁ¶ÇØ¾ß Çϸç, API»ç¿ë¹æ¹ýÀº Æ®¸®ÀÇ
°³³äÀ» ¾Ë°í ÀÖ´Ù¸é ÀÇ¿Ü·Î °£´ÜÇÏ´Ù.

¢º Simple DOM Programming with Java
// DOMSample.java

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;

public class DOMSample {
    public static void main(String [] args) {
        Document doc;
        try{
            // DOM Document¸¦ »ý¼ºÇϱâ À§ÇÏ¿© ÆÑÅ丮¸¦ »ý¼ºÇÑ´Ù
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setIgnoringElementContentWhitespace(true);
            // ÆÑÅ丮·ÎºÎÅÍ DocumentÆļ­¸¦ ¾ò¾î³»µµ·Ï ÇÑ´Ù.
            DocumentBuilder builder = factory.newDocumentBuilder();
            
            // Document DOMÆļ­·Î ÇÏ¿©±Ý ÀԷ¹ÞÀº ÆÄÀÏÀ» ÆĽÌÇϵµ·Ï ¿äûÇÑ´Ù.
            doc = builder.parse(new File(args[0]));

            //ÆÄ½ÌµÈ ÈÄÀÇ ·çÆ®¿ä¼Ò¸¦ ¾ò¾î³»º¸µµ·Ï Çϸç, ±× ŸÀÔÀº ElementŸÀÔÀÌ´Ù.
            Element root = doc.getDocumentElement();

            //ÁÖ¼®À» Çѹø Ãß°¡ÇÏ¿© ´Þ¾Æº»´Ù.
            Comment comment = doc.createComment("Training test");
            // ¸¸µé¾îÁø ÁÖ¼®À» ·çÆ®¿ø¼ÒÀÇ ÀÚ½ÄÀ¸·Î Ãß°¡ÇÑ´Ù.
            root.appendChild(comment);
            
            // ·çÆ®¿ø¼ÒÀÇ °¢Á¾ Á¤º¸¸¦ Ãâ·ÂÇغ¸µµ·Ï ÇÑ´Ù
         System.out.println("Tag name : " + 
                               root.getTagName());
         System.out.println("Node type : " +
                                  root.getNodeType());
         System.out.println("First Child : " + 
                                  root.getFirstChild().getNodeValue());

            // ÇöÀç ¸Þ¸ð¸®¿¡ ¿Ã¶ó¿Í ÀÖ´Â DOM Document¸¦ È­¸é¿¡ Ãâ·ÂÇغ»´Ù.
            // ´Ù¸¥ ¿¹¸¦ ÆÄÀÏ Ãâ·Âµµ °¡´ÉÇÏ´Ù.
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.transform(new DOMSource(doc), new StreamResult(System.out));
        }catch(Exception e) {
            System.out.println(e);
        }
    }
};
Sample.xml <?xml version="1.0"> <address-book> <person> <name>Choi ji woong</name> <address>Seoul</address> <phone>011-xx</phone> </person> <person> <name>jeon hong seong</name> <address>Suji</address> <phone>016-xx</phone> </person> </address-book> ½ÇÇà ¹æ¹ý set SAMPLE_HOME=´ç½ÅÀÌ ¼³Ä¡ÇÑ XMLÆļ­¸¦ À§ÇÑ jarÆÄÀÏÀÌ ÀÖ´Â µð·ºÅ丮 javac -classpath .;%SAMPLE_HOME%\crimson.jar;%SAMPLE_HOME%\xalan.jar; %SAMPLE_HOME%\jaxp.jar;%SAMPLE_HOME%\xerces.jar -d . %1 %2 %3 %4 %5 java -classpath .;%SAMPLE_HOME%\crimson.jar;%SAMPLE_HOME%\xalan.jar; %SAMPLE_HOME%\jaxp.jar;%SAMPLE_HOME%\xerces.jar -d . %1 %2 %3 %4 %5 ÀÌ»óÀ¸·Î ¸Å¿ì °£´ÜÇÏ°Ô DOM¿¡ ´ëÇÑ °³¿ä¸¦ º¸¾Ò´Ù. ¸ðµç °ÍµéÀÌ ±×·¯ÇϵíÀÌ ÀÌ·¸°Ô ±Û·Î Ç¥ÇöÇÑ´Ù´Â °ÍÀº »ó´çÈ÷ ÈûµçÀÏÀÌ´Ù. ÀÌ ÀÌ»ó¿¡ ´ëÇÑ °ÍÀº ¿©·¯ºÐµéÀÌ Á÷Á¢ Æļ­¸¦ ´Ù¿î¹Þ¾Æ ¼³Ä¡ÇÏ°í ¼ÒÀ§ "¸Ç¶¥¿¡ ÇìµùÇغ¸´Ù"¶ó´Â °Íó·³ ¹®¼­ Àо°í ÇØ°áÁ¡À» ã¾Æ³»°í API¸¦ ´Ù·êÁÙ ¾Ë¾Æ¾ß ÇÏ´Â °ÍÀº ¹°·ÐÀÌ´Ù. Á» ´õ ³ª¾Æ°¥ ¹Ù¸¦ ¿øÇÑ´Ù¸é ÀÚ½ÅÀÇ ³ë·Â¾øÀÌ´Â ºÒ°¡´ÉÇÒ °Í °°´Ù. ÀÌ»óÀ¸·Î DOM¿¡ ´ëÇÑ ¼Ò°³¸¦ ¸¶Ä£´Ù.
 
1
References
 
Xerces : http://xml.apache.org/xerces2-j/index.html
Sun XML : http://java.sun.com/xml
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD