IBM Korea Skip to main content
       IBM Ȩ    |  Á¦Ç° & ¼­ºñ½º  |  °í°´Áö¿ø & ´Ù¿î·Îµå  |  È¸¿ø°¡ÀÔ  

ÀÚ¹Ù ÇÁ·Î±×·¡¹ÖÀ¸·Î ¿Â¶óÀÎ XML µ¥ÀÌÅÍ °Ë»öÇϱâ
XML ÆĽÌ

Level: Introductory

Dan Becker
Software developer, IBM Software Group
2002³â 9¿ù

ÀÎÅͳݿ¡ Á¤º¸¸¦ ÆÛºí¸®½ÌÇÏ´Â °ÍÀÌ º¸ÆíÈ­ µÇ¾î °¡¸é¼­ ÀÌ·¯ÇÑ Á¤º¸¸¦ ¹ß°ßÇÏ°í ¿äûÇÏ´Â °Í ¶ÇÇÑ ÀÚ¿¬½º·¯¿î ÀÏÀÌ µÇ¾ú´Ù. ÀÌ ±ÛÀº ÀÚ¹Ù ÇÁ·Î±×·¡¹ÖÀ» »ç¿ëÇÏ¿© À¥ ±â¹Ý XML µ¥ÀÌÅ͸¦ ¾ò°í µ¥ÀÌÅ͸¦ ÆĽÌÇÏ¿© ÇÊ¿äÇÑ ¿¤¸®¸ÕÆ®¿Í ¼Ó¼ºµéÀ» ÇÊÅ͸µÇÏ°í ¿äûµÈ Á¤º¸¸¦ ÀÌ¿ëÇÏ¿© ÀÛ¾÷À» ¼öÇàÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÑ´Ù.

ÀÌ ±ÛÀº 2002³â 8¿ù IBM developerWorks Àú³Î¿¡ ±âÀçµÇ¾ú´Ù.

ÀÎÅͳݿ¡ Á¤º¸¸¦ ³»º¸³»´Â °ÍÀÌ ÀÚ¿¬½º·¯¿î Çö»óÀÎ ¸¸Å­ Á¤º¸¸¦ ¹ß°ßÇÏ°í ¿äûÇÏ´Â °Íµµ ÀÚ¿¬½º·¯¿î ÀÏÀÌ µÇ¾ú´Ù. XMLÀº ºñÁî´Ï½º¿Í ¼ÒºñÀÚ°¡ Á¤º¸¸¦ ´õ¿í ½±°Ô °øÀ¯ÇÒ ¼ö ÀÖµµ·Ï µ¥ÀÌÅ͸¦ ¼³¸íÇϴµ¥ »ç¿ëÇÒ ¼ö ÀÖ´Â ±â¼ú Áß ÇϳªÀÌ´Ù. À¥ »óÀÇ XML Á¤º¸ÀÇ ¿¹Á¦¿¡´Â ³¯¾¾ Á¤º¸, Áõ±Ç ½Ã¼¼Ç¥, ¼±ÀûÇ¥, Ç×°ø¿ä±ÝÇ¥, °æ¸Å°¡°Ý, ¿À´ÃÀÇ À¯¸Ó °°Àº Á¤º¸µéÀ» Æ÷ÇÔÇÏ°í ÀÖ´Ù. ÀÌ µ¥ÀÌÅÍ¿¡ ¿Ö ¾×¼¼½ºÇÏ·Á Çϴ°¡? ¾Æ¸¶µµ ÀÚ±â Áö¿ªÀÇ ³¯¾¾ µ¥ÀÌÅ͸¦ ÀúÀåÇÏ°í °Ë»öÇϰųª, ¼±Àû °úÁ¤À» Á¶»ç ÇÒ ÀÛÀº À¯Æ¿¸®Æ¼¸¦ ÀÛ¼ºÇØ¾ß ÇÒ ÀÌÀ¯¿¡¼­ ÀÏ °ÍÀÌ´Ù. ¿©·¯ºÐÀÌ ÀÚ¹Ù °³¹ßÀÚ¶ó°í °¡Á¤ÇÑ´Ù¸é ÆĽ̰ú Á¶ÀÛÀÌ ½¬¿î XML Á¤º¸¸¦ ãÀ»°ÍÀÌ´Ù.

¹°·Ð HTML ÆäÀÌÁö°¡ ÀÖÁö¸¸ ´ëºÎºÐÀÇ µ¥ÀÌÅÍ´Â XML Æ÷¸Ë¿¡¼­ ºñ·ÔµÇ°í À¥ ¼­¹ö¿¡´Â HTML·Î º¯È¯µÈ´Ù. ¸¹Àº À¥ »çÀÌÆ®µéÀº µÎ Æ÷¸ËÀ¸·Î Á¤º¸¸¦ Á¦°øÇÑ´Ù. ±¸ ºê¶ó¿ìÀú¿Í µ¥ÀÌÅ͸¦ ´Ü¼øÈ÷ °Ë»öÇÏ°íÀÚ ÇÏ´Â »ç¶÷µé¿¡°Ô´Â HTML·Î Á¦°øµÇ°í, µ¥ÀÌÅÍ ¸ðÀ½°ú ºÐ¼®À» ¿øÇÏ´Â ³Ý Á¤Åë ÇÁ·Î±×·¡¸Ó¿¡°Ô´Â XML Æ÷¸ËÀ¸·Î Á¦°øµÈ´Ù. HTML ÆäÀÌÁö¿¡¼­ µ¥ÀÌÅ͸¦ °¡Á®¿À´Â °Í º¸´Ù´Â ÀÚ¹Ù ÇÁ·Î±×·¡¹ÖÀ¸·Î XML Á¤º¸¸¦ ÆĽÌÇÏ°í ¸ðÀ¸´Â °ÍÀÌ ÈξÀ ´õ ½±´Ù. ¿ì¼± Ç¥ÁØ XML Æļ­°¡ ÀÖÀ¸¸é ´Ù¿î·ÎµåÇϱⰡ ½±´Ù. µÑ°, ¹®¼­ ±¸Á¶´Â ½Ã°£ÀÌ È帣¸é¼­ º¯Çϱ⠶§¹®¿¡ HTML ÅÂ±× º¸´Ù´Â XML ¿¤¸®¸ÕÆ®¿Í ¼Ó¼ºÀ¸·Î ³ªÅ¸³»´Â °ÍÀÌ ´õ¿í ½±´Ù.

½ÃÀÛÇϱâ
XML µ¥ÀÌÅ͸¦ ÆĽÌÇϱâ À§Çؼ­´Â XML Æļ­°¡ ÀÖ¾î¾ß ÇÑ´Ù. ¹°·Ð ¿©·¯ºÐ Á¤µµ¶ó¸é °¢ÀÚÀÇ Æļ­¸¦ ÀÛ¼ºÇÒ ¼ö ÀÖ°ÚÁö¸¸ ¿Ïº®ÇÑ ±â´ÉÀÇ Ç¥ÁØ Æļ­µéÀÌ ¹«·á·Î ³ª¿ÍÀÖ´Ù. Java 2 Platform, version 1.3 Enterprise Edition (J2EE)µµ À¯¿ëÇÒ °ÍÀÌ´Ù. Æļ­´Â °³¹ßÀÚÀÇ ½Ã°¢¿¡¼­ º¼ ¶§ ¼Óµµ³ª ½Å·Ú¼º¿¡¼­ Â÷ÀÌ°¡ ³ªÁö¸¸ ÇÁ·Î±×·¡¹Ö ¸ðµ¨°ú ÀÎÅÍÆäÀ̽º´Â µ¿ÀÏÇÏ´Ù. °Ô´Ù°¡ ¹«·áÀÌ´Ù.

ÀÚ¹Ù °³¹ßÀÚ¶ó¸é javax.xml ÆÐÅ°Áö¿¡ ÁÖÀǸ¦ ±â¿ï¿©¾ß ÇÑ´Ù. ÀÌ ÆÐÅ°Áö¿¡´Â XML µ¥ÀÌÅ͸¦ ÆĽÌÇϴµ¥ ÇÊ¿äÇÑ ¸ðµç Äڵ尡 ÀÖ´Ù. XML ¹®¼­¸¦ ´Ù¸¥ Çü½ÄÀ¸·Î º¯ÇüÇÏ´Â ÆÐÅ°Áö¿Í ÇÔ²² Æļ­ ±¸Çö ÆÐÅ°ÁöµéÀÌ ÀÖ´Ù.

DOM vs SAX Æļ­
µÎ °¡Áö À¯ÇüÀÇ XML ¹®¼­ Æļ­°¡ ÀÖ´Ù. À̵éÀº XML ¹®¼­¿¡ ¾×¼¼½ºÇÏ´Â ¹æ½ÄºÎÅÍ ´Ù¸£´Ù:

  • Document Object Model (DOM). XML ¹®¼­·ÎÀÇ ·£´ý ¾×¼¼½º(random access)¿¡ »ç¿ëµÈ´Ù. DOMÀÇ ÀåÁ¡Àº ¸Þ¸ð¸® ¾È¿¡ ¹®¼­ÀÇ Àüü ¸ðµ¨À» °®°í ÀÖ´Ù´Â Á¡ÀÌ´Ù. ÀÌ´Â ¸ðµç XML ¿¤¸®¸ÕÆ®¿¡ ¾î¶² ¼ø¼­·ÎµçÁö ¾×¼¼½º°¡ °¡´ÉÇÏ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. ÇÏÁö¸¸ Å« ¹®¼­ÀÇ °æ¿ì µÐÇØ Áú ¼ö ÀÖ´Ù. ¸Þ¸ð¸®¿¡¼­ ½ÇÇà½ÃÅ°Áö ¾ÊÀ¸¸é ½Ã½ºÅÛÀÌ ÇÑ°è¿¡ ´Ù´Ù¸¦ ¶§ ÆÛÆ÷¸Õ½º°¡ ´À·ÁÁø´Ù.

  • Simple API for XML (SAX). ¼øÂ÷Àû ¾×¼¼½º(sequential access)¿¡ »ç¿ëµÈ´Ù. SAXÀÇ ÀåÁ¡Àº ¹®¼­ÀÇ ÇÑ ºÎºÐÀÌ ¸Þ¸ð¸®¿¡¼­ »ç¿ëµÉ ¼ö Àֱ⠶§¹®¿¡ º¸´Ù Å« ¹®¼­¸¦ ÇÚµéÇÒ ¼ö ÀÖ´Ù´Â Á¡ÀÌ´Ù. SAXÀÇ ´ÜÁ¡Àº ¿¤¸®¸ÕÆ®¸¦ ¼ø¼­´ë·Î ó¸®ÇØ¾ß Çϸç Çѹø¿¡ º¼ ¼ö ÀÖ´Â ¹®¼­ ºÎºÐµµ ÀÛ´Ù. SAX¸¦ ÀÌ¿ëÇÏ¿© ¹®¼­¸¦ ÆĽÌÇÒ ¶§ XML ºÎºÐÀ» ÀúÀåÇÒ ¼ö ÀÖ´Ù.

¿Â¶óÀÎ Á¤º¸ »çÀÌÆ®(³¯¾¾ µ¥ÀÌÅÍ ¶Ç´Â ÁÖ½Ä ÇöȲ)¿¡ ¾×¼¼½ºÇÏ´Â XML ¹®¼­´Â ÀÛ°í ÁýÁߵǾî ÀÖ´Â °æÇâÀÌ ÀÖ´Ù. ÀϹÝÀûÀ¸·Î ƯÁ¤ »çÀÌÆ®³ª ÁÖ½Ä µ¥ÀÌÅ͸¦ °Ë»öÇÏ°í Á¤º¸¸¦ ó¸®ÇÏ°í ´ÙÀ½ Äõ¸® ¶Ç´Â ¹®¼­°¡ °í·ÁµÈ´Ù. ÀÌ·¯ÇÑ À¯ÇüÀÇ ¾×¼¼½ºÀÇ °æ¿ì DOMÀº ¼±È£µÇ´Â ÆÄ½Ì ¹æ½ÄÀÌ´Ù. ¹Ý¸é SAX ÆĽÌÀº ÀÌ ºÐ¾ß°¡ ¾Æ´Ï´Ù.

XML ¹®¼­ ¾ò±â
Æļ­°¡ ÀÖÀ¸¹Ç·Î À¥¿¡¼­ XML ¹®¼­¿¡ ¾×¼¼½º ÇÒ ¼ö ÀÖ´Ù. Listing 1Àº ÀÎÅͳݿ¡¼­ ³¯¾¾ µ¥ÀÌÅ͸¦ °¡Á®¿À´Â ¿¹Á¦ÀÌ´Ù:

Listing 1. XML ³¯¾¾ µ¥ÀÌÅÍ ¿¹Á¦

<forexml site="Austin">
<observation
    city="Austin, Texas"
    temp.F="75"
    rel_hum.percent="31"
    wind_speed.knt="8"
    skies="partly cloudy"
/>
<almanac
    sunrise="7:08 AM"
    sunset="6:21 PM"
/>
<forecast
    type="nearterm"
    source="NWS"
    day="THIS AFTERNOON"
    weather="SU"
    high_temp="77"
    text="HIGHS 75 TO 80. WEST 15 TO 20 MPH."
/>
</forexml>

Unisys CorporationÀº National Oceanic and Atmospheric Administration¿Í ¼ö¹é°³ÀÇ ³¯¾¾ °üÃø »çÀÌÆ®¿Í Çù·ÂÇÏ¿© www.weather.unisys.com/forexml.cgi¿¡ XML Æ÷¸ËÀ¸·Î ±â»ó °üÃøÀ» ³»º¸³»°í ÀÖ´Ù. URL (¿¹¸¦ µé¾î, www.weather.unisys.com/forexml.cgi?Austin) ´ÙÀ½¿¡ ¿äû Çʵ忡 µµ½Ã À̸§ °°Àº °üÃø Áö¿ªÀÇ À̸§À» ÀÔ·ÂÇ϶ó. ¹°À½Ç¥´Â ¿äû Çʵ带 URL¿¡ Ãß°¡Çϴµ¥ ÇÊ¿äÇÑ °æ°è±âÈ£ ÀÌ´Ù. ÀÐÀ» ¼ö ÀÖ´Â °üÃø °á°ú°¡ XML Æ÷¸ËÀ¸·Î ¸®ÅϵȴÙ. (Listing 1). J2EE¿¡ Á¦°øµÈ XML Æļ­¸¦ »ç¿ëÇغ¸ÀÚ.

°³³ä»óÀ¸·Î XML ¹®¼­¸¦ DOMÀ¸·Î º¼ ¼ö ÀÖ´Ù. forexml ³ëµå´Â ¹®¼­ÀÇ ³ëµå Æ®¸®¿¡¼­ »Ñ¸® ³ëµå (root node)·Î¼­ º¸¿©Áø´Ù. »Ñ¸® ³ëµå´Â observation, almanac, forecast °°Àº ÀÚ½Ä ³ëµå¸¦ °®°í ÀÖ´Ù. ÀÌµé ³ëµå °¢°¢ observation À̳ª 0 ¶Ç´Â ±× ÀÌ»óÀÇ ¿¤¸®¸ÕÆ®¸¦ °®°í ÀÖ´Ù. ¿¹¸¦µé¾î observation ¿¤¸®¸ÕÆ®´Â 75 °ªÀ» °¡Áø temp.F ¼Ó¼ºÀ» °®°í ÀÖ´Ù. ±×¸² 1Àº ¹®¼­ ³ëµåÀÇ °èÃþÀ» ³ªÅ¸³½´Ù:

±×¸²1. ¹®¼­ ³ëµå °èÃþ(¸¼Àº ³¯!)
Hierarchy of document nodes (A pleasant day!)

ÁÖ¾îÁø XML ¹®¼­¿¡¼­ °ü½ÉÀÖ´Â ¿¤¸®¸ÕÆ®¿Í ¼Ó¼ºÀ» ¾î¶»°Ô ÇÊÅ͸µ ÇÒ±î? ¿ì¼± ÆĽÌÇÏ°íÀÚ ÇÏ´Â ÀÎÀÚ¸¦ ¸ð¾Æ¾ß ÇÑ´Ù. Listing 2´Â ¸í·ÉÇà¿¡¼­ ÇÁ·Î±×·¥ ÀÎÀÚ¸¦ ¸ðÀ¸°í ±×µéÀ» processDocument¶ó°í ÇÏ´Â ¸ÞÀÎ ÇÁ·Î¼¼½Ì ¸Þ¼Òµå·Î Àü´ÞÇϴ ǥÁØ main ¸Þ¼Òµå¸¦ º¸¿©ÁÖ°í ÀÖ´Ù. Java applet°ú HTML ¸Å°³º¯¼ö·Î ºñ½ÁÇÑ ÀÛµ¿À» ¼öÇàÇÒ ¼ö ÀÖ´Ù. Listing 2¿¡¼­ ÄÚµå´Â Ç¥ÁØÀÌ°í XML ÇÁ·Î±×·¡¹ÖÀÌ ¾ø´Ù. ´ÙÀ½¿¡ ¿Â´Ù.

Listing 2. ³¯¾¾ ¾ÖÇø®ÄÉÀÌ¼Ç ¸ÞÀÎ ¸Þ¼Òµå

// Given a list of arguments, parse a city
// name and a list of attributes to filter.
// Alternatively, accept "all" as an indication
// to get all attributes

public static void main( String [] args ) {
   if ( args.length == 0) {
     System.out.println
       ( "Usage: java Weather city"
           +" [all|attributes]" );
     System.out.println
       ( "Example:"
           +" java Weather Austin temp.F" );
     System.exit( 0 );
   }
   // Gather arguments
   String city = args[ 0 ];
   String [] attrNames = null;
   if ( args.length == 1) {
     attrNames = new String [] { "all" };
   } else {
     // args.length > 1
     attrNames =
       new String [ args.length - 1 ]; 
     for ( int i = 1; i < args.length; i++)
       attrNames[ i - 1 ] = args[ i ];
   } // if
	
   // Process document
   processDocument( city, observationTagName,
     attrNames );
} // main

Listing 3Àº XML ¹®¼­¸¦ ¾ò¾î À̸¦ ÆĽÌÇϴµ¥ ¾²ÀÌ´Â ÀüÇüÀûÀÎ ±¸Á¶ÀÌ´Ù:

Listing 3. DocumentBuilder ¿äûÇϱâ

// Given a city string, an element name, and
// a list of attributes, print the values of
// the attributes found in the XML document.
public static void processDocument(
   String city,
   String elementName, String [] attrNames ) {
   DocumentBuilderFactory dbf =
      DocumentBuilderFactory.newInstance ( ) ;
      
   try {
         DocumentBuilder db =
            dbf.newDocumentBuilder ( ) ;
            
         if ( city != null ) {
             String urlString =
                 weatherSite + city;
             Document document = getDocument
                ( db, urlString );
             if ( document != null ) {
                Node [] matchNodes =
                   getAttributes( document,
                      elementName, attrNames );
                 if (null != matchNodes ) {
                   if ( matchNodes.length > 6 )
                       printNodes( "City=" + city,
                         matchNodes, true );
                   else  
                       printNodes( "City=" + city,
                         matchNodes, false );
                 } else 
                     System.out.println (  
                        "Element \"" +
                        elementName +
                   "\" not found in document." );
         } else
             System.out.println 
                ( "No XML created from URL="
                   + urlString );
   } // if         
  } catch (  
      ParserConfigurationException e ) { 
      e.printStackTrace ( ) ; 
  }
} // processDocument

¸ÕÀú DocumentBuilderFactoryÀÇ ÀνºÅϽº¸¦ ¾ò´Â´Ù. ÀÏ´Ü ÆÑÅ丮 °´Ã¼¸¦ ¾òÀ¸¸é »õ·Î¿î Document Builder ¸Þ¼Òµå¸¦ »ç¿ëÇÏ¿© DocumentBuilder¸¦ ¾ò´Â´Ù. ÀÌ°ÍÀº XML ÀÎDz ½ºÆ®¸²À» ÆĽÌÇÒ ¼ö ÀÖ´Â °´Ã¼ÀÌ´Ù. ¸®ÅϵǴ ½ÇÁ¦ DocumentBuilderFactory °´Ã¼´Â ´ÙÀ½ ¼³Á¤¿¡ ÀÇÁ¸ÇÑ´Ù:

  • javax.xml.parsers.DocumentBuilder Factory system property
  • JAVA_HOME/lib/jaxp.properties
  • META-INF/services/javax.xml.parsers.DocumentBuilderFactory ¼­ºñ½º(jar file)
  • platform default

J2EE¸¦ ¼³Ä¡ÇÒ ¶§ µðÆúÆ® ¼³Á¤À» »ç¿ëÇÑ´Ù¸é ÀÌ ¿¹Á¦´Â ¿Ã¹Ù¸£°Ô ÀÛµ¿ÇѴ٠Ưº°ÇÑ Æļ­ ±â´ÉÀ» ÀÌ¿ëÇÏ°íÀÚ ÇÒ ¶§¿¡´Â ´Ù¸¥ Æļ­¸¦ ÁöÁ¤ÇÑ´Ù. ¿©±â¼­ ¾òÀ» ¼ö ÀÖ´Â ±³ÈÆÀº ¸¹Àº ÀÚ¹Ù XML ÇÁ·Î¼¼¼­¸¦ °¡Áö°í ÀÛ¾÷Ç϶ó´Â °ÍÀÌ´Ù.

Listing 3 ÀÇ ÄÚµå ³ª¸ÓÁö ºÎºÐÀº ¼Ó¼º°ú ÀÏÄ¡ÇÏ´Â XML ¹®¼­¸¦ ã¾Æ ÇÁ¸°Æ®ÇÑ´Ù. ÀÌ°ÍÀº °í±Þ ¸Þ¼Òµå ÀÌ´Ù. URL¿¡¼­ ¹®¼­¸¦ ¾ò°í ÇÊÅ͸µÇÏ°í ¼Ó¼ºÀ» ÇÁ¸°Æ®ÇÏ´Â °Í¿¡ ´ëÇÑ ¼³¸íÀº ´ÙÀ½ ¼½¼Ç¿¡ ÀÖ´Ù.

Listing 4´Â À¥ ¿¡¼­ XML ¹®¼­¸¦ ¾ò°í Listing 3¿¡¼­ getDocument ¸Þ¼Òµå¸¦ °Ë»öÇÏ´Â °ÍÀ» º¸¿©ÁÖ°í ÀÖ´Ù:

Listing 4. ¿äûµÈ URL¿¡¼­ XML ¹®¼­ ÆĽÌÇϱâ

// Using the given document builder object,
// construct and return an XML DOM
// from the given URL.

public static Document getDocument
  ( DocumentBuilder db, String urlString ) {
   try {
     URL url = new URL( urlString );
     
     try {
       URLConnection URLconnection =
          url.openConnection ( ) ;
       HttpURLConnection httpConnection =
          (HttpURLConnection)
         URLconnection;
         
       int responseCode =
        httpConnection.getResponseCode ( ) ;
       if ( responseCode ==
        HttpURLConnection.HTTP_OK) {
           InputStream in =
            httpConnection.getInputStream ( ) ;
            
           try {
             Document doc = db.parse( in );
             return doc;
           } catch(
            org.xml.sax.SAXException e ) {
                e.printStackTrace ( ) ;
           }
        } else {
            System.out.println
               ( "HTTP connection response !=
               HTTP_OK" );
               
        } 
     } catch ( IOException e ) { 
          e.printStackTrace ( ) ;
     } // Catch
  } catch ( MalformedURLException e ) {  
      e.printStackTrace ( ) ;
  } // Catch
  return null;
} // getDocument

¿ì¼± URLConnection Àº URL ½ºÆ®¸µ¿¡ ¿­·ÁÀÖ´Ù. ³¯¾¾ »çÀÌÆ®°¡ HTTP ¿äûÀ» ÅëÇؼ­ ÀÛµ¿ÇÏ´Â °ÍÀ» ¾Ë°í Àֱ⠶§¹®¿¡ ´Ü¼øÇÑ URLConnection¸¦ HttpURLConnection¿¡ ij½ºÆÃÇÒ ¼ö ÀÖ´Ù. ±×·± ´ÙÀ½, ¼­¹ö¿¡¼­ ¿Â ÀÀ´ä Äڵ带 Å×½ºÆ®ÇÑ´Ù. ¿äû¿¡ ¿¡·¯°¡ ¾ø´Ù´Â °ÍÀ» ¼­¹ö°¡ Ç¥½ÃÇϸé Ä¿³Ø¼Ç ½ºÆ®¸²À» ¿­°í ÀÀ´äÀ» XML ½ºÆ®¸²À¸·Î Ãë±ÞÇÑ´Ù. DocumentBuilder ÆĽº ¸Þ¼Òµå´Â ÆĽÌÇÏ°í µ¥ÀÌÅÍ ½ºÆ®¸²À» javax.xml. ¹®¼­·Î µ¹¸°´Ù. ÀÌ °úÁ¤ Áß ¾î¶² °ÍÀÌ¶óµµ ½ÇÆÐÇÏ¸é ¸Þ¼Òµå´Â XML ¹®¼­ °´Ã¼¸¦ ¸¸µé ¼ö ¾ø´Ù´Â °ÍÀ» ³ªÅ¸³»´Â nullÀ» ¸®ÅÏÇÑ´Ù.

¿øÇÏ´Â ¿¤¸®¸ÕÆ®¸¦ ÇÊÅ͸µÇϱâ
¿©·¯ ¿¤¸®¸ÕÆ® ű׵éÀÌ root ű×ÀÎ forexml ´ÙÀ½¿¡ ³ª¿Ô´Ù´Â °ÍÀ» ±â¾ïÇ϶ó:

  • observation. ³¯¾¾ °üÃø Áö¿ª ¹× Àå¼Ò, ¿Âµµ, ½Àµµ, dz¼Ó µîÀÇ µ¥ÀÌÅÍ Æ÷ÇÔ.
  • almanac. °üÃø Àå¼ÒÀÇ ÀÏÃâ°ú Àϸô µîÀÇ µ¥ÀÌÅÍ Æ÷ÇÔ.
  • forecast. ¾ÕÀ¸·ÎÀÇ ³¯¾¾ ¿¹»ó Æ÷ÇÔ.

Listing 5´Â observation ű׸¦ »ç¿ëÇÏ°í ´Ù¸¥ ű׵éÀº ¹«½ÃµÈ´Ù:

Listing 5. XML ¿¤¸®¸ÕÆ®¿Í ¼Ó¼º ¸ÂÃß±â

// Given an XML document,
// return the values of all attributes
// for the given element.

public static Node [] getAttributes
   ( Document document,
    String elementName, String [] attrNames ) {
    
    // Get elements with the given tag name
    // (matches on * too)
    NodeList nodes = document.getElementsByTagName
       ( elementName );
    if ( nodes.getLength() < 1) {
      return null;
    }
    
    Node firstElement = nodes.item( 0 );
    NamedNodeMap nnm =
       firstElement.getAttributes ( ) ;
   
    if (nnm != null) {
      // Test the value of each attribute
      Node [] matchNodes = new Node
         [ attrNames.length ];
        
       for (int i = 0; i < attrNames.length; i++){
          boolean all =
            attrNames[ i ].equalsIgnoreCase("all");
           if (all) {
             // named node map
             int nnmLength = nnm.getLength();
             matchNodes = new Node[ nnmLength ];
             
             for ( int j = 0; j < nnmLength; j++){
                matchNodes[ j ] = nnm.item( j );
             }
             return matchNodes;
           } else {
               matchNodes[ i ] = nnm.getNamedItem
                 ( attrNames[ i ] );
               if ( matchNodes[ i ] == null ) {
                 matchNodes[ i ] =
                     document.createAttribute
                     ( attrNames[ i ] );
                  ((Attr)matchNodes[ i ]).setValue
                    ( unknownAttribute );
               }
           } // if
      } // for
      
      return matchNodes;
   } // if
   
   return null;
} // printDocumentAttrs

Document Ŭ·¡½º°¡ getElementsByTagName ¸Þ¼Òµå¸¦ °¡Áö°í ÀÖ´Â °Í¿¡ ÁÖ¸ñÇ϶ó. observation ¿¤¸®¸ÕÆ® À̸§À» »ç¿ëÇÏ¿© ¸Þ¼Òµå´Â ¹®¼­ÀÇ ´Ù¸¥ ¿¤¸®¸ÕÆ®¿¡¼­ ¿äûµÈ ű׸¦ ÇÊÅ͸µÇÏ°í NodeList °´Ã¼¸¦ ¸®ÅÏÇÑ´Ù.

ÄÚµå´Â ù ¹ø° ¿¤¸®¸ÕÆ®¸¦ ¼±ÅÃÇÏ°í getAttributesÀ» »ç¿ëÇÏ¿© ±× ¿¤¸®¸ÕÆ®¿ë XML ¼Ó¼º ¸®½ºÆ®¸¦ ¾ò´Â´Ù. ¼Ó¼ºÀº NamedNodeMap °´Ã¼¿¡ ¸®ÅϵȴÙ. ÀÌ·¯ÇÑ °¢°¢ÀÇ ³ëµåµéÀº ¿äûµÈ ¼Ó¼º À̸§ÀÌ ¸Â´ÂÁö ¶Ç´Â ¿ÍÀϵå Ä«µå°¡ ¸ðµÎ¿Í ¸Â´ÂÁö¸¦ È®ÀÎÇϱâ À§ÇØ Å×½ºÆ®µÈ´Ù. ÀÌ ¿¹Á¦´Â ³ëµå °´Ã¼µéÀÇ ¾î·¹À̸¦ ±¸ÇöÇÏ°í ÀÌ°ÍÀ» caller¿¡°Ô ¸®ÅÏÇÑ´Ù. ¿¤¸®¸ÕÆ®³ª ¼Ó¼ºÀ» ãÁö ¸øÇÏ¸é ¸Þ¼Òµå´Â nullÀ» ¸®ÅÏÇÑ´Ù.

ÀÚ¹Ù ÇÁ·Î±×·¡¹ÖÀ¸·Î ¿Â¶óÀÎ XML µ¥ÀÌÅ͸¦ Á¶ÀÛÇÏ´Â ÁÖ¿ä ´Ü°è´Â ´ÙÀ½À¸·Î ¿ä¾àµÉ ¼ö ÀÖ´Ù:

  1. DocumentBuilderFactory¸¦ ÀÌ¿ëÇÏ¿© XML Æļ­ ¿äûÇϱâ
  2. DocumentBuilder¸¦ ÀÌ¿ëÇÏ¿© XML Æļ­ ¹× ±¸ÇöÇϱâ
  3. ¿äûµÈ ¿¤¸®¸ÕÆ®¿Í ¼Ó¼º ã±â

¹ß°ßÇÑ µ¥ÀÌÅ͸¦ ¾î¶»°Ô ÀÌ¿ëÇÒ °ÍÀΰ¡´Â ÀÌÁ¦ ¿©·¯ºÐ¿¡°Ô ´Þ·ÁÀÖ´Ù. ´ÙÀ½ ¼½¼Ç¿¡¼­´Â XML µ¥ÀÌÅ͸¦ Çڵ鸵ÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÏ°Ú´Ù.

XML µ¥ÀÌÅÍÀÇ ÇÁ¸°ÆÃ
Listing 6Àº ³ëµåµéÀ» ÅëÇØ ¹Ýº¹ÇÏ°í ¼Ó¼º µ¥ÀÌÅ͸¦ ÇÁ¸°Æ®ÇÏ´Â ¹æ¹ýÀÌ´Ù:

Listing 6. XML ¼Ó¼º µ¥ÀÌÅ͸¦ ÇÁ¸°Æ®ÇÏ´Â ¸Þ¼Òµå ¿¹Á¦

// Given a set of nodes,
// print the values of the nodes.
// The nodes may be given a title.
// The nodes may be printed on one line
// (grouped) or many.

public static void printNodes
  ( String title, Node [] nodes,
       boolean grouped ) {
       
       // Report the name value of each node
       System.out.print( title );
       
       if (grouped){
          System.out.println ( ) ;
       }
       // Walk through the nodes.
       for ( int i = 0; i < nodes.length; i++ ) {
           Node node = nodes[ i ];
           System.out.print
              ( grouped ? " " : ", " );
           System.out.print
              ( node.getNodeName() + "=" +
              node.getNodeValue() );
              
            if ( grouped ) {
               System.out.println ( ) ;
            }  
    } // for
    
    if (!grouped) {
        System.out.println ( ) ;
    }  
} // printAttributeValues

Listing 5 ¿¡¼­´Â ĸÃÄÇÏ°íÀÚÇÏ´Â XML ¿¤¸®¸ÕÆ®¿Í ¼Ó¼º µ¥ÀÌÅ͸¦ ¸ÂÃç ³ëµå ¾î·¹À̸¦ ¸¸µç´Ù. ÀÌ·¯ÇÑ Æ¯Á¤ÇÑ ÇÁ¸°Æ® ·çƾÀº ÇÑ ¶óÀÎ ¶Ç´Â ¿©·¯ ¶óÀο¡ °ÉÃÄ ¸ðµç ¼Ó¼ºµéÀ» ÇÁ¸°Æ®ÇÒ ¼ö ÀÖ´Ù.

ÀÌÁ¦ ¿©ÇàÀÇ ³¡¿¡ ¿Í ÀÖ´Ù. Listing 7Àº ÆĽÌÇÏ°í ¼±ÅÃÇÑ XML ¼Ó¼º µ¥ÀÌÅÍ ¾Æ¿ôDz ¿¹Á¦ÀÌ´Ù:

Listing 7. ÀÚ¹Ù ³¯¾¾ ÇÁ·Î±×·¥ ½ÇÇà¿¡ µû¸¥ ¾Æ¿ôDz ¿¹Á¦

City=Austin
   city=Austin, Texas
   latitude=30.30
   longitude=-97.70
   time=3 PM CDT ...
   temp.F=84
   temp.C=28
   rel_hum.percent=58
   wind.string=S at 11 knt
   skies=clear    

ÀÌ °æ¿ì µ¥ÀÌÅ͸¦ System.out À¸·Î ÇÁ¸°Æ®ÇßÁö¸¸ °¢ÀÚÀÇ ¾ÖÇø®ÄÉÀ̼ǿ¡¼­´Â µ¥ÀÌÅ͸¦ µ¥ÀÌÅͺ£À̽º¿¡ ÀúÀåÇÒ ¼ö ÀÖ°í Æ®·£µå ºÐ¼®À̳ª ±×·¡ÇÁ ±×¸®±âµµ °¡´ÉÇÏ´Ù. ÀÌ·¯ÇÑ ³¯¾¾ °üÃøÀº ¸Å ½Ã°£ ¾÷µ¥ÀÌÆ® µÇ¾î ÁÖ±âÀûÀÎ Á¡°ËÀÌ °¡´ÉÇÏ´Ù.

ÀÌ ÇÁ·Î±×·¥À» »ç¿ëÇÏ¿© ¸ðµç °üÃø ¼Ó¼ºµéÀ» ã°Å³ª ¿Âµµ ¶Ç´Â »ó´ë ½Àµµ, À§µµ¿Í °æµµ °°Àº ¼­ºê¼Âµµ ¿äûÇÒ ¼ö ÀÖ´Ù!

°á·Ð
ÀÚ¹Ù ÇÁ·Î±×·¡¹ÖÀ» ÀÌ¿ëÇÏ¿© ¿Â¶óÀÎ XML µ¥ÀÌÅ͸¦ °Ë»öÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇß´Ù. °¡Àå Áß¿äÇÑ ´Ü°è´Â °ü½ÉÀÖ´Â ³»¿ëÀÌ ¼ö·ÏµÇ¾îÀÖ´Â »çÀÌÆ®¸¦ ã´Â °ÍÀÌ´Ù. ÀÏ´Ü »çÀÌÆ®¸¦ ¹ß°ßÇÏ¸é µ¥ÀÌÅÍ ÃßÃâ ÀÛ¾÷Àº ¸ðµç XML ¹®¼­ÀÇ °æ¿ì °°´Ù. ¿ì¼± ¹®¼­¸¦ ¿äûÇÏ°í ±×·± ´ÙÀ½ ÆĽÌÇÑ´Ù. ¸¶Áö¸·À¸·Î ¿øÇÏ´Â ¿¤¸®¸ÕÆ®¿Í ¼Ó¼ºÀ» ÇÊÅ͸µÇÑ´Ù. Ç¥ÁØ XML Æļ­¸¦ ÀÌ¿ëÇϸé Á÷Á¢ ÀÛ¼ºÇÏ´Â °Í º¸´Ù °­·ÂÇÑ ÅøÀ» °¡Áú ¼ö ÀÖ´Ù.

Âü°íÀÚ·á

¸ñ Â÷:
½ÃÀÛÇϱâ
DOM vs SAX Æļ­
XML ¹®¼­ ¾ò±â
¿øÇÏ´Â ¿¤¸®¸ÕÆ®¸¦ ÇÊÅ͸µÇϱâ
XML µ¥ÀÌÅÍ ÇÁ¸°Æ®Çϱâ
°á·Ð
Âü°í ÀÚ·á
ÇÊÀÚ ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
Subscribe to the developerWorks newsletter
Understanding JTS, Part 2
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
Also in the Java zone:
Tutorials
Tools and products
Code and components
Articles
Also in the XML zone:
Tutorials
Tools and products
Code and components
Articles
ÇÊÀÚ¼Ò°³
Dan Becker´Â IBM(Austin, Texas)ÀÇ ¼ÒÇÁÆ®¿þ¾î ±×·ì¿¡¼­ ÀÏÇÏ°í ÀÖ´Ù. ÇöÀç´Â Interactive Financial Services ÇÁ·ÎÁ§Æ®¿¡¼­ ¿Â¶óÀÎ À¥ ¹ðÅ· ½Ã½ºÅÛ ÀÛ¾÷À» ¼öÇàÇÏ°í ÀÖ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

Á¤¸» ÁÁ´Ù (5) ÁÁ´Ù (4) ±×Àú±×·¸´Ù (3) ¼öÁ¤º¸¿ÏÀÌ ÇÊ¿äÇÏ´Ù(2) ÇüÆí¾ø´Ù (1)

  È¸»ç¼Ò°³  |  °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥  |  ¹ý·ü  |  ¹®ÀÇ