EJB(xml/mail/jms/jdbc)
2016.01.29 / 20:17

XML ¹®¼­ »ý¼ºÇÏ¿© Ãâ·ÂÇϱâ

amazona
Ãßõ ¼ö 223

 1. DOMImplementationLSÀÇ ÀνºÅϽº¸¦ Ãëµæ
   DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
   DOMImplementationLS domImpLS = (DOMImplementationLS) registry.getDOMImplementation("LS 3.0");

   2. Document
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();       
   Document document = factory.newDocumentBuilder().newDocument();

   3. Root element(ex : news¶ó´Â ·çÆ® ¿¤¸®¸ÕÆ®¸¦ »ý¼º)
   Element rootElem = document.createElement("news");

   4. System element(ex : new¶ó´Â ¿¤¸®¸ÕÆ®´Â links, mytext¶ó´Â ¼Ó¼ºÀ» °®°í, °¢°¢ÀÇ ¼Ó¼º°ªÀº detail.action?page=1, titleÀÌ´Ù.)
   //for (int i = 0; i < list.size(); i++) {  (¿©·¯°³ÀÇ ¿¤¸®¸ÕÆ®¸¦ »ý¼ºÇÒ °æ¿ì)
   Element systemElem = document.createElement("new");
   systemElem.setAttribute("links", "detail.action?page=1");
   systemElem.setAttribute("mytext", "title");
   rootElem.appendChild(systemElem); //(À§¿¡¼­ »ý¼ºÇÑ news¶ó´Â ·çÆ®¿¤¸®¸ÕÆ®¿¡ Ãß°¡)
   //}
   document.appendChild(rootElem);

   5. LSOutput »ý¼º
   LSOutput output = domImpLS.createLSOutput();
   output.setEncoding("Shift_JIS");
   output.setByteStream(new FileOutputStream("test.xml")); //Ãâ·Â ÆÄÀϸíÀ» ¼³Á¤

   6. LSSerializerÀÇ »ý¼º°ú XML¹®¼­ÀÇ Ãâ·Â
   LSSerializer serializer = domImpLS.createLSSerializer();
   serializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); //XML ¹®¼­ Ãâ·Â½Ã °³Çà µîÀ» ³Ö¾î º¸±âÁÁ°Ô Ãâ·ÂÇÏ°í ½ÍÀº °æ¿ì, format-pretty-print ÆĶó¹ÌÅÍ °ªÀ» TRUE·Î ¼³Á¤.
ÀÌ °ªÀ» ¼³Á¤ÇÏÁö ¾ÊÀ¸¸é °³Çà¾øÀÌ ÇÑÁÙ·Î Ãâ·ÂµÊ.

   serializer.write(document, output);

--------------------------------------------------------------------
°á°ú¹° : test.xml ÆÄÀÏÀÌ »ý¼ºµÊ
test.xml ³»¿ë :

 <?xml version="1.0" encoding="Shift_JIS"?>
< news>
    <new links="detail.action?page=1" mytext="title"/>
< /news>