Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


XMLÀ» ÀÌ¿ëÇÑ Database Query Manager ±¸ÇöÇϱâ
 
À̹ø ±Û¿¡¼­´Â Áö±Ý±îÁö º¸¾Æ¿Ô´ø ³»¿ëÀ» Åä´ë·Î ÇÏ¿© Á» ¾Õ¼­ ³ª°¡´Â ¸éÀÌ ¾øÁö ¾ÊÁö¸¸ ½ÇÁ¦ Spider WAF¿¡¼­ »ç¿ëµÇ´Â query manager¸¦ ±¸ÇöÇغ¸µµ·Ï ÇÏ°Ú´Ù. ( 2003/03/17 ) 488
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 


À̹ø ±Û¿¡¼­´Â Áö±Ý±îÁö º¸¾Æ¿Ô´ø ³»¿ëÀ» Åä´ë·Î ÇÏ¿© Á» ¾Õ¼­ ³ª°¡´Â ¸éÀÌ ¾øÁö ¾ÊÁö¸¸
½ÇÁ¦ Spider WAF¿¡¼­ »ç¿ëµÇ´Â query manager¸¦ ±¸ÇöÇغ¸µµ·Ï ÇÏ°Ú´Ù.

¢º XML Query Manager¸¸µé¾îº¸±â

¸ÕÀú ¿Ö ÀÌ·±°ÍÀÌ ¸¸µé¾îÁö°Ô µÇ¾ú´ÂÁöºÎÅÍ »ìÆ캸µµ·Ï ÇÏÀÚ.
XMLÀÌ Á¡Â÷ ´ëµÎµÇ°í EJB1.1 specºÎÅÍ deployment descriptor°¡ XML¹®¼­·Î ÀÛ¼ºÀÌ µÇ±â ½ÃÀÛÇϸ鼭
EJB Container¿¡ ´ëÇÑ È¯°æµéÀÌ server start½Ã¿¡ µ¿ÀûÀ¸·Î ÀоîµéÀÏ ¼ö Àִ ȯ°æ°ú ȯ°æÀÌ
¹Ù²î´õ¶óµµ ½ÇÁ¦ ¼Ò½ºÄÚµå´Â ¹Ù²îÁö ¾ÊÀ¸¸ç text confÆÄÀÏÀÇ º¯°æÀ¸·Î ÀÛµ¿ÀÌ µÉ ¼ö ÀÖµµ·Ï 
ÇÏ´Â °æÇâÀ¸·Î ¹Ù²î¾ú´Ù.

ÀÌ°ÍÀÌ ¹«¾ùÀÌ´ø°¡? ±â¾ïÀÌ ³ª´Â°¡? ¹Ù·Î loosely couplingÀÌ´Ù. °´Ã¼¿Í °´Ã¼°£ÀÇ ¿¬°á°ü°è¸¦
Ä¿´Ù¶õ °³³ä¿¡¼­ ¹Ù¶óº¸Áö ¸øÇÑ´Ù¸é ±×°ÍÀº tightlyÇÏ°Ô ¿¬°áµÉ °ÍÀÌ°í, ¼öÁ¤ÀÌ ¶ÇÇÑ ÇÁ·Î±×·¥ÀÇ
¼Ò½ºº¯°æ±îÁö ÀÏÀ¸Å°°Ô ÇÏ´Â À¯¿¬¼º¾ø´Â ±¸Á¶°¡ µÇ¾î¹ö¸±°ÍÀÌ´Ù.

Spider WAF¿¡µµ Àû¿ëµÇ¾î ÀÖ´Ù°í À̾߱âÇß´Ù. ¿©±â¼­´Â ½ÇÁ¦ ÇÁ·ÎÁ§Æ®ÁøÇà½Ã¿¡ »ç¿ëµÇ´Â Äõ¸®´Â
ÇÁ·Î±×·¥ÀÇ Data Access Object¾È¿¡ ÄÚµùÇÏ¿© ³Ö´Â °ÍÀÌ ¾Æ´Ï¶ó engineÀÌ startµÇ¾îÁö´Â ½ÃÁ¡¿¡
Äõ¸®¸¦ startup servlet¸¦ ÅëÇÏ¿© ÀÐ¾î µéÀÎ ÈÄ ÇØ´ç Äõ¸®¸¦ DOM Object·Î, ´Ù½Ã DOM Object¸¦ 
ÀÚ¹ÙÀÇ ¿ì¸®°¡ ½±°Ô Çؼ®Çس¾ ¼ö ÀÖ´Â °´Ã¼ ÇüÅ·ΠÀо¾î ó¸®Çϵµ·Ï ÇÏ¿´´Ù.

ÇÏÁö¸¸ À̹ø ¾ÆƼŬÀÇ Äڵ忡¼­´Â Á÷Á¢ DOM Document¸¦ Á¶»çÇÏ¿© ÇØ´ç Äõ¸®µéÀ» insert, update, 
delete, selectÇÏ´Â ¿¹¸¦ µé°Ú´Ù.

±×·¸´Ù¸é Áö±Ý °¡Àå ¸ÕÀú ÁغñÇØ¾ß ÇÒ °ÍÀÌ ¹«¾ùÀΰ¡? 
XMLÀ» ¾´´Ù°í ÇßÀ¸´Ï Æļ­°¡ ´ç¿¬È÷ ÇÊ¿äÇÒ °ÍÀÌ°í, ÀÚ¹ÙÂÊÀÇ °´Ã¼°¡ ÇÊ¿äÇÏ´Ù ÇßÀ¸´Ï JDKµµ
ÇʼöÀÏ°ÍÀÌ´Ù.
¿©·¯ºÐµéÀÌ ¸¸¾à JDK1.4ÀÌ»óÀÇ ¹öÁ¯À» °¡Áö°í ÀÖ´Ù¸é Æļ­°¡ ³»ÀåµÇ¾î Àֱ⠶§¹®¿¡ ÇÊ¿ä°¡ ¾øÁö¸¸
³·Àº ¹öÁ¯ÀÇ JDK¶ó¸é XercesÆļ­³ª JAXPÆļ­¸¦ ´Ù¿î¹Þ¾Æ ¼³Ä¡Çϵµ·Ï ÇÏÀÚ.

¼³Ä¡¹æ¹ýÀ» ¸ð¸£´Â°¡? ¾Æ·¡ÀÇ ¾ÆƼŬÀ» Àо±â ¹Ù¶õ´Ù.

http://www.javapattern.info/viewArticle.do?articleId=1047437421002

ÀÚ. ¶Ç ¹«¾ùÀÌ ÇÊ¿äÇÑ°¡? ¿£Áø¾î¼±¸ ¶°µé¾úÀ¸´Ï ¿£ÁøÀÌ ÇÊ¿äÇÑ °ÍÀΰ¡? ±×°ÍÀº ¾Æ´Ï´Ù.
¼±ÅûçÇ×ÀÏ »ÓÀÌ´Ù. ¿¹Á¦¿¡¼­´Â ³î»õÀÇ ÁÖƯ±âÀÎ ÀÏ¹Ý ¾ÖÇø®ÄÉÀ̼ÇÀ¸·Î À¥À» Èä³»³»º¼ °ÍÀÌ´Ù.

ÇÁ·Î±×·¥À» ÀÛ¼ºÇÒ Áغñ°¡ ¸ðµÎ µÇ¾ú´Â°¡? ±×·¸´Ù¸é ´Ù½Ã ÇÑ ¹ø ¹°¾îº¸°Ú´Ù.
¹«¾ùºÎÅÍ ÀÛ¼ºÇÒ °ÍÀΰ¡?

ÀÌ°ÍÀº ¿©·¯ºÐµéÀÌ ³ª¸§´ë·Î »ý°¢ÇÏ¿©µµ µÈ´Ù. ²À ÀÌ°ÍÀÌ Á¤´äÀÌ´Ù ¶ó´Â °ÍÀº ¾ø±â ¶§¹®ÀÌ´Ù.

¿ì¼±  ½Ã³ª¸®¿À¸¦ ¼³¸íÇÏ°Ú´Ù.

1. ÇÁ·ÎÁ§Æ®ÆÀÀÌ ÄÚµå»ó¿¡ µ¥ÀÌÅͺ£À̽ºÄõ¸®¸¦ ³ÖÁö ¾Êµµ·Ï ÇÑ´Ù.
2. Äõ¸®¸¦ ÀúÀåÇÑ ÀúÀå¼Ò·Î´Â fileÇüÅÂÀÇ xml document¸¦ »ç¿ëÇÑ´Ù.
XMLÀÇ ±¸Á¶´Â ·çÆ®¿ø¼Ò·Î Database¸íÀÌ ¿À°Ô µÇ¸ç ÀÚ½ÄÀ¸·Î table¸í, Äõ¸®µéÀÌ ¿Àµµ·Ï ÇÑ´Ù.
3. ºó¹øÇÑ IO¹ß»ýÀ» ÁÙÀ̱â À§ÇÏ¿© DOM Tree¸¦ ¸Þ¸ð¸®¿¡ ·ÎµåÇÏ¿© »ç¿ëÇÑ´Ù.
4. XML󸮿¡ ÇÊ¿äÇÑ utility¼º Ŭ·¡½º¸¦ ÀÛ¼ºÇÑ´Ù.


À§ÀÇ 2¹ø¿¡¼­ XMLµµÅ¥¸ÕÆ®¸¦ »ç¿ëÇÑ´Ù ÇÏ¿´À¸¹Ç·Î XML¿¡ ´ëÇÑ Á¤ÀǸ¦ ³»·Áº¸°Ú´Ù. ¸ÕÀú DTDºÎÅÍ ÀÛ¼ºÇÏ¿©¾ß ÇÏ°ÚÁö¸¸ »ý·«Çϵµ·Ï ÇÏ°í Á÷Á¢ ¿¹Á¦ »ùÇÃÀ» ¿Ã·Áº¸µµ·Ï ÇÏ°Ú´Ù. <?xml version="1.0" encoding="MS949"?> <oracle> <sample> <insert>INSERT INTO poll VALUES (seq_poll_id.NEXTVAL, ?, ?, ?, ?, ?, ?, ?, 0)</insert> <update> UPDATE poll SET poll_count = poll_count + 1 WHERE poll_id = ? </update> <delete /> <select>select * from carouser</select> </sample> </oracle> À§ÀÇ °æ¿ìó·³ Á¤ÀǸ¦ Çϵµ·Ï ÇÏÀÚ. ·çÆ®¿ø¼Ò·Î oracleÀÌ Á¤ÀǵǾî ÀÖÀ¸¸ç, sampleÀ̶õ Å×ÀÌºí¿¡ CRUDÄõ¸®°¡ Á¤ÀǵǾî ÀÖµµ·Ï ÇÑ´Ù. ÀÚ, ÀÌÈÄ¿¡´Â ¾î¶°ÇÑ Å¬·¡½º¸¦ ÀÛ¼ºÇØ¾ß ÇÏ´Â °ÍÀϱî? À§ÀÇ 4¹ø¿¡¼­ À̾߱âÇÑ utilityŬ·¡½º¸¦ ÀÛ¼ºÇØ¾ß ÇÒ°ÍÀε¥, °Å±â¿¡ µé¾î°¥ ¸Þ¼Òµåµé·Î´Â xml¿¡¼­ query¸¦ ¾ò¾î¿À´Â ¸Þ¼Òµå, xml document¸¦ Á¶ÀÛ(node insert, update, delete)ÇÏ´Â ¸Þ¼ÒµåµéÀÌ ÀÖ¾î¾ß ÇÒ °ÍÀÌ´Ù. º¸Åë ÀϹÝÀûÀΠȯ°æ¼³Á¤ÀÇ xml°°Àº °æ¿ì´Â ÇÏÀ§³ëµå 3´Ü°è ÀÌÇϷδ ÁøÇàµÇ´Â ¼ºÇâÀÌ ¾ø´Ù. ¿Ö³ÄÇϸé NodeÀÇ depth°¡ ±í¾îÁö¸é ±í¾îÁú¼ö·Ï ½ÇÁ¦ »ç¶÷ÀÌ ´«À¸·Î ½±°Ô Æǵ¶ÇϱⰡ ¾î·Á¿öÁö±â ¶§¹®ÀÌ´Ù. ±×·¡¼­ º¸Åë À¯Æ¿¸®Æ¼¼º Ŭ·¡½º¸¦ ÀÛ¼ºÇÏ°Ô µÇ¸é 3´Ü°èÀÇ node±îÁö Á¶ÀÛÇÒ ¼ö ÀÖ´Â ÇÁ·Î±×·¥À» ¸¹ÀÌ Â¥°Ô µÇ´Âµ¥ ¾Æ·¡ÀÇ XmlDAO°°Àº °æ¿ìµµ ±×·± 3´Ü°è depth¿¡ ´ëÇÑ Ã³¸®¸¦ ÁÖ·ÎÇÏ´Â ¸Þ¼Òµåµé·Î Á¤ÀǵǾî ÀÖ´Ù. /** * QueryManger.java * Written date : 2002/05/10 * Author : Choi ji woong */ package com.carouser.spider.manager; import org.xml.sax.InputSource; import org.w3c.dom.Element; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Text; import org.apache.crimson.tree.XmlDocument; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.SAXException; import java.io.Reader; import java.net.URL; import java.util.Vector; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; // jaxp 1.0.1 imports import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; public class XmlDAO { /** * ÇØ´ç URI¸¦ ÀÔ·Â¹Þ¾Æ xmlÀÇ ³»¿ëÀ» InputSource¿¡ ÀÔ·ÂÀ¸·Î ³Ö¾î DOMÆļ­¸¦ ÀÌ¿ëÇÏ¿© ÆĽÌÇÑ´Ù. * @param location ÇØ´ç xmlÀÇ À§Ä¡ * @return Element */ public static Element loadDocument(File location) { Document doc = null; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); doc = parser.parse(location); Element root = doc.getDocumentElement(); root.normalize(); /* //Output to standard output ; use Sun's reference imple for now XmlDocument xdoc = (XmlDocument) doc; xdoc.write(new OutputStreamWriter(System.out)); */ return root; } catch (SAXParseException err) { System.err.println ("URLMappingsXmlDAO ** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.err.println("URLMappingsXmlDAO error: " + err.getMessage ()); } catch (SAXException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (java.net.MalformedURLException mfx) { System.err.println("URLMappingsXmlDAO error: " + mfx); } catch (java.io.IOException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (Exception pce) { System.err.println("URLMappingsXmlDAO error: " + pce); } return null; } /** * ÇØ´ç URI¸¦ ÀÔ·Â¹Þ¾Æ xmlÀÇ ³»¿ëÀ» InputSource¿¡ ÀÔ·ÂÀ¸·Î ³Ö¾î DOMÆļ­¸¦ ÀÌ¿ëÇÏ¿© ÆĽÌÇÑ´Ù. * @param location ÇØ´ç xmlÀÇ À§Ä¡ * @return Element */ public static Element loadDocument(String location) { Document doc = null; try { URL url = new URL(location); InputSource xmlInp = new InputSource(url.openStream()); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); doc = parser.parse(xmlInp); Element root = doc.getDocumentElement(); root.normalize(); return root; } catch (SAXParseException err) { System.err.println ("URLMappingsXmlDAO ** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.err.println("URLMappingsXmlDAO error: " + err.getMessage ()); } catch (SAXException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (java.net.MalformedURLException mfx) { System.err.println("URLMappingsXmlDAO error: " + mfx); } catch (java.io.IOException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (Exception pce) { System.err.println("URLMappingsXmlDAO error: " + pce); } return null; } /** * ÇØ´ç URI¸¦ ÀÔ·Â¹Þ¾Æ xmlÀÇ ³»¿ëÀ» InputSource¿¡ ÀÔ·ÂÀ¸·Î ³Ö¾î DOMÆļ­¸¦ ÀÌ¿ëÇÏ¿© ÆĽÌÇÑ´Ù. * @param target java.io.Reader * @return Element */ public static Element loadDocument(Reader target) { Document doc = null; try { InputSource xmlInp = new InputSource(target); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); doc = parser.parse(xmlInp); Element root = doc.getDocumentElement(); root.normalize(); return root; } catch (SAXParseException err) { System.err.println ("URLMappingsXmlDAO ** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ()); System.err.println("URLMappingsXmlDAO error: " + err.getMessage ()); } catch (SAXException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (java.net.MalformedURLException mfx) { System.err.println("URLMappingsXmlDAO error: " + mfx); } catch (java.io.IOException e) { System.err.println("URLMappingsXmlDAO error: " + e); } catch (Exception pce) { System.err.println("URLMappingsXmlDAO error: " + pce); } return null; } /** * Element¿Í ãÀ» ű׸¦ ÀÔ·Â¹Þ¾Æ ±× ű×ÀÇ text node¸¦ return * @param root root element * @param tagName ã°íÀÚÇϴ ű׸í * @return String */ public static String getTagValue(Element root, String tagName) { String returnString = ""; NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); if (node != null) { Node child = node.getFirstChild(); if ((child != null) && child.getNodeValue() != null) return child.getNodeValue(); } } return returnString; } /** * Element¿Í ãÀ» ű׸¦ ÀÔ·Â¹Þ¾Æ ±× ű×ÀÇ text value¸¦ return * @param root root element * @param tagName ã°íÀÚÇϴ ű׸í * @param subTagName 3´Ü°è ÇÏÀ§Å±׸í * @return String */ public static String getSubTagValue( Element root, String tagName, String subTagName) { String returnString = ""; NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); if (node != null) { NodeList children = node.getChildNodes(); for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) { Node child = children.item(innerLoop); if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { Node grandChild = child.getFirstChild(); if (grandChild.getNodeValue() != null) return grandChild.getNodeValue(); } } // end inner loop } } return returnString; } /** * Element¿Í ãÀ» ű׸¦ ÀÔ·Â¹Þ¾Æ ±× ű×ÀÇ attribute¸¦ return * @param root root element * @param tagName ã°íÀÚÇϴ ű׸í * @param subTagName 3´Ü°è ÇÏÀ§Å±׸í * @param attribute ã°íÀÚÇÏ´Â ¾ÖÆ®¸®ºäÆ®¸í * @return String */ public static String getSubTagAttribute( Element root, String tagName, String subTagName, String attribute) { String returnString = ""; NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); if (node != null) { NodeList children = node.getChildNodes(); for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) { Node child = children.item(innerLoop); if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { if (child instanceof Element) { return ((Element)child).getAttribute(attribute); } } } // end inner loop } } return returnString; } private static String getSubTagValue(Node node, String subTagName) { String returnString = ""; if (node != null) { NodeList children = node.getChildNodes(); for (int innerLoop =0; innerLoop < children.getLength(); innerLoop++) { Node child = children.item(innerLoop); if ((child != null) && (child.getNodeName() != null) && child.getNodeName().equals(subTagName) ) { Node grandChild = child.getFirstChild(); if (grandChild.getNodeValue() != null) return grandChild.getNodeValue(); } } // end inner loop } return returnString; } /** * ³ëµåÀÇ À§Ä¡ºÎºÐ¿¡ ÇØ´çÇÏ´Â Å×À̺í¸í°ú Äõ¸®Å¸ÀÔ°ú °ªÀ» »ðÀÔÇÑ´Ù. * @param root root element * @param table Å×À̺í xmlÅÂ±× * @param queryType insert, update, deleteµîÀÇ Äõ¸®Å¸ÀÔ */ public static void nodeSubInsert( Element root, String table, String queryType, String queryValue){ Node find = getTag(root, table); Document doc = find.getOwnerDocument(); Element type = doc.createElement(queryType); Text value = doc.createTextNode(queryValue); type.appendChild(value); find.appendChild(type); print(doc); } /** * ³ëµåÀÇ À§Ä¡ºÎºÐ¿¡ ÇØ´çÇÏ´Â Å×À̺í¸í°ú Äõ¸®Å¸ÀÔ°ú °ªÀ» °»½ÅÇÑ´Ù. * @param root root element * @param table Å×À̺í xmlÅÂ±× * @param queryType insert, update, deleteµîÀÇ Äõ¸®Å¸ÀÔ */ public static void nodeSubUpdate( Element root, String table, String queryType, String queryValue) { Node find = getTag(root, table); Document doc = find.getOwnerDocument(); NodeList nl = find.getChildNodes(); int numnodes = nl.getLength(); System.out.println("length : " + numnodes); Node replNode = null; for(int i = 0 ; i < numnodes ; i++) { Node n = nl.item(i); String name = n.getNodeName(); if(name.equals(queryType)) { replNode = n; System.out.println("Finding Node : " + replNode.getNodeName()); break; } } Element type = doc.createElement(queryType); Text value = doc.createTextNode(queryValue); type.appendChild(value); find.replaceChild(type, replNode); print(doc); } /** * ³ëµåÀÇ À§Ä¡ºÎºÐ¿¡ ÇØ´çÇÏ´Â Å×À̺í¸í°ú Äõ¸®Å¸ÀÔ°ú °ªÀ» »èÁ¦ÇÑ´Ù. * @param root root element * @param table Å×À̺í xmlÅÂ±× * @param queryType insert, update, deleteµîÀÇ Äõ¸®Å¸ÀÔ */ public static void nodeSubDelete( Element root, String table, String queryType) { Node find = getTag(root, table); Document doc = find.getOwnerDocument(); NodeList nl = find.getChildNodes(); int numnodes = nl.getLength(); System.out.println("length : " + numnodes); Node deleteNode = null; for(int i = 0 ; i < numnodes ; i++) { Node n = nl.item(i); String name = n.getNodeName(); if(name.equals(queryType)) { deleteNode = n; System.out.println("Finding Node : " + deleteNode.getNodeName()); break; } } find.removeChild(deleteNode); print(doc); } // carouser util append public static Node getTag(Element root, String tagName) { NodeList list = root.getElementsByTagName(tagName); for (int loop = 0; loop < list.getLength(); loop++) { Node node = list.item(loop); System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType()); if (node.getNodeName().equals(tagName)) { return node; } } return null; } /** * È­¸é»ó¿¡ XML Document¸¦ Ãâ·ÂÇÑ´Ù. */ private static void print(Document doc) { try{ XmlDocument xdoc = (XmlDocument) doc; xdoc.write(new OutputStreamWriter(System.out)); }catch(Exception e) {} } /** * ÆÄÀÏ¿¡ XML Document¸¦ Ãâ·ÂÇÑ´Ù. */ public static void fileSave(Element root, String location) { System.out.println("root : " + root.getClass().getName()); Document doc = (org.w3c.dom.Document) root.getOwnerDocument(); System.out.println("doc : " + doc.getClass().getName()); try{ File f = new File(location); FileOutputStream fos = new FileOutputStream(f); XmlDocument xdoc = (XmlDocument) doc; xdoc.write(new OutputStreamWriter(fos)); }catch(Exception e) {} } } À§¿¡¼­´Â ÀϹÝÀûÀ¸·Î XML Document¸¦ traverseÇϸç Á¶ÀÛÇÏ´Â ÀüÇüÀûÀÎ utilityŬ·¡½º¸¦ ÀÛ¼ºÇß´Ù. ±âº»ÀûÀÎ API¸¦ ¿©·¯ºÐµéÀÌ Àß ¾Ë°í ÀÖ´Ù¸é Äڵ带 ÀÌÇØÇϴµ¥ ±×¸® ¾î·Á¿òÀÌ ¾øÀ» °ÍÀÌ´Ù. ¹ü¿ëÀûÀÎ ¸Þ¼Òµå·Î´Â loadDocument() getTagValue() getSubTagValue() getSubTagAttribute() getTag() µîÀÌ »ç¿ëµÉ ¼ö ÀÖÀ¸¸ç À̹ø ½Ã³ª¸®¿À¿¡¼­ µ¶Ã¢ÀûÀ¸·Î »ç¿ëµÇ´Â ¸Þ¼Òµå´Â À§ÀÇ ¸Þ¼Òµå¸¦ Á¦¿ÜÇÑ ÀüºÎÀÌ´Ù. À§ÀÇ print()¸Þ¼Òµå¿Í fileSave()¸Þ¼Òµå´Â crimsonÆÐÅ°Áö°¡ ÀÖ¾î¾ß °¡´ÉÇÏÁö¸¸ ´Ù¸£°Ô ÄÚµùÀ» ÇÑ´Ù¸é xml transformer¸¦ ÀÌ¿ëÇÏ¿© Ãâ·ÂÇÏ´Â ¹æ¹ýÀ» ´õ ±ÇÀåÇϵµ·Ï ÇÏ°Ú´Ù. xalanÆÐÅ°Áö¸¦ ÀÌ¿ëÇÑ transformer´Â Ãâ·ÂÀ» ¾Æ·¡¿Í°°ÀÌ ÇÒ ¼ö ÀÖ´Ù. // xalan.jar import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; public static void write(Element root) { try{ Document doc = root.getOwnerDocument(); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("encoding", "euc-kr"); //transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "euc-kr"))); transformer.transform(new DOMSource(doc), new StreamResult(new FileWriter("oracle.xml"))); }catch(Exception e) { e.printStackTrace(); } } ÀÚ, ±×·¯¸é ÀÌÁ¦ Äõ¸®¸¦ manageÇϴ Ŭ·¡½º¸¦ ÀÛ¼ºÇغ¸µµ·Ï ÇÏÀÚ. package com.carouser.spider.manager; import java.util.Hashtable; import java.io.FileInputStream; import org.w3c.dom.Element; import org.xml.sax.InputSource; import java.io.File; public class QueryManager { private static QueryManager manager; private static Element queryRoot; private Hashtable xmlList; private static boolean alive = false; private static String location = "oracle.xml"; static { getInstance(); } private QueryManager() {} public static String getQuery(String table, String element){ checkInstance(); return XmlDAO.getSubTagValue(queryRoot, table, element); } public static void insert(String table, String queryType, String queryValue) { checkInstance(); XmlDAO.nodeSubInsert(queryRoot, table, queryType, queryValue); } public static void update(String table, String queryType, String queryValue) { checkInstance(); XmlDAO.nodeSubUpdate(queryRoot, table, queryType, queryValue); } public static void delete(String table, String queryType) { checkInstance(); XmlDAO.nodeSubDelete(queryRoot, table, queryType); } public static void fileSave() { checkInstance(); XmlDAO.fileSave(queryRoot, location); } private static void checkInstance() { if( !alive ) getInstance(); } private static void getInstance() { if( manager == null ) { synchronized(QueryManager.class) { manager = new QueryManager(); alive = true; } } queryRoot = XmlDAO.loadDocument(new File(location)); } } À§ÀÇ Äڵ忡¼­´Â Å×½ºÆ®Çϱâ À§ÇÏ¿© º°´Ù¸¥ °ÍÀÌ ¾ø´Ù. ½ÇÁ¦ ¸Þ¸ð¸®»ó¿¡ xml¹®¼­°¡ ·ÎµåµÇ¾ú´ÂÁö¸¦ üũÇÏ¿© selectÇÒ°ÍÀÎÁö ¾Æ´Ï¸é insert, update, deleteÇÒ°ÍÀÎÁö¸¦ °áÁ¤ÇÏ¿© XmlDAO À¯Æ¿¸®Æ¼ Ŭ·¡½ºÀÇ ¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿© ½ÇÁ¦ xmlÀ» Á¶ÀÛÇÏ´Â callÀ» ÇÒ »ÓÀÌ´Ù. °£´ÜÇÑ ÄÚµå¶ó ÃæºÐÈ÷ ÀÌÇØÇÒ °ÍÀ̶ó »ý°¢ÇÑ´Ù. ÀÌÁ¦ Å×½ºÆ® Ŭ·¡½º¸¦ ÀÛ¼ºÇغ¸µµ·Ï ÇÑ´Ù. import com.carouser.spider.manager.QueryManager; public class ManagerTest{ public static void main(String[] args) { String select = QueryManager.getQuery("sample", "select"); System.out.println("Select Query : " + select); //insert sample //QueryManager.insert("sample", "result", "none"); // update sample QueryManager.update("sample", "select", "select * from carouser"); // delete sample //QueryManager.delete("sample", "select"); // save file QueryManager.fileSave(); } } À§ÀÇ °æ¿ì¿¡¼­´Â ¸ðµç °æ¿ì¿¡ ´ëÇÏ¿© Å×½ºÆ®ÇÏ°í ÀÖ´Ù. ¿©·¯ºÐµéÀÌ ÁÖ¼®À» Áö¿ö¼­ ó¸®ÇÏ¸é µÉ°ÍÀÌ´Ù. insert(), update(), delete(), getQuery(), fileSave()µîÀ» Å×½ºÆ®Çغ¼ ¼ö ÀÖÀ¸¸ç, ÀÌ·¯ÇÑ ¹æ¹ýÀ» ÀÌ¿ëÇÑ´Ù¸é QueryAdmin°°Àº ¸Þ´º¸¦ À¥»ó¿¡¼­ ¸¸µé¾î Å×½ºÆ®Çغ¼ ¼ö ÀÖÀ» °ÍÀÌ´Ù. XMLÀ̶õ°Ô SAXÀÇ À̺¥Æ®¿Í DOMÀÇ Tree¸¦ ÀÌÇØÇÏ°í Á¶ÀÛÇÒ ¼ö ÀÖ´Ù¸é ó¸®ÇÏ´Â °ÍÀº ±²ÀåÈ÷ ½¬¿îÀÏÀÌ¸ç ´ÜÁö ¾î·Æ´Ù´Â °ÍÀÌ ÀÌ·±°ÍµéÀ» È®ÀåÇÑ À¥¼­ºñ½º¶óµçÁö ÇÁ·ÎÅäÄÝ¿¡ ´ëÇÑ °ÍÀÌ ÀÖÀ» ¼ö Àִµ¥ ±×°ÍÀº XMLÀÚü°¡ ¾Æ´Ï¶ó specificÇÑ ÇüÅÂÀÇ °³³äÀ» »ç¿ëÇϱ⠶§¹®¿¡ °³³äÁ¢±ÙÀÌ ¾î·Á¿ö¼­¶ó°í º¼ ¼ö ÀÖ´Ù. Å×½ºÆ®ÇÒ ¼ö ÀÖ´Â ÆÄÀÏÀº referenceÀÇ Ã·ºÎÆÄÀϷΠ÷ºÎÇÏ°Ú´Ù.
 
1
References
 
¾ÆƼŬ¼Ò½º : QueryManager.zip
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD