개발자의 끄적끄적

[Java] XML 예제 [펌] 본문

개발/java & jsp

[Java] XML 예제 [펌]

효벨 2020. 5. 26. 03:00
728x90
반응형

[Java] XML 예제 [펌]

 

 

XML을 구성하고 구성한 XML을 스트링으로 변환하여 출력

 

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class Test {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception{
  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
  Document doc = parser.newDocument();
  
  Element root = doc.createElement("root");
  doc.appendChild(root);
  
  Element custom = doc.createElement("custom");
  custom.setTextContent("text");
  root.appendChild(custom);
  
  
  TransformerFactory factory = TransformerFactory.newInstance();
  Transformer former = factory.newTransformer();
  former.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  former.setOutputProperty(OutputKeys.INDENT, "yes");
  
  StringWriter sw = new StringWriter();
  StreamResult result = new StreamResult(sw);
  DOMSource source = new DOMSource(doc);
  former.transform(source, result);
  System.out.println(sw.toString());
 }

}

 

출력결과:

 <root>
<custom>text</custom>
</root>

 

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class Test {

    /**
      * @param args
      */
     public static void main(String[] args) throws Exception{
      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
      Document doc = parser.newDocument();
     
      Element root = doc.createElement("graph");
      root.setAttribute("formatNumberSacle", "0");
      root.setAttribute("decimalPrecision", "0");
      root.setAttribute("yAxisName", "");
      root.setAttribute("xAxisName", "");
      root.setAttribute("caption", "0");
     
      doc.appendChild(root);
     
      Element custom_1 = doc.createElement("set");
      custom_1.setAttribute("color", "AFD8F8");
      custom_1.setAttribute("value", "95");
      custom_1.setAttribute("name", "재미있는 강의");
      root.appendChild(custom_1);
     
      Element custom_2 = doc.createElement("set");
      custom_2.setAttribute("color", "F6BD0F");
      custom_2.setAttribute("value", "43");
      custom_2.setAttribute("name", "자세한 설명");
      root.appendChild(custom_2);
     
      Element custom_3 = doc.createElement("set");
      custom_3.setAttribute("color", "8BBA00");
      custom_3.setAttribute("value", "53");
      custom_3.setAttribute("name", "요점중심");
      root.appendChild(custom_3);
     
     
      TransformerFactory factory = TransformerFactory.newInstance();
      Transformer former = factory.newTransformer();
      former.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
      former.setOutputProperty(OutputKeys.INDENT, "yes");
     
      StringWriter sw = new StringWriter();
      StreamResult result = new StreamResult(sw);
      DOMSource source = new DOMSource(doc);
      former.transform(source, result);
      System.out.println(sw.toString());
     }

}

출력결과:

 

   <?xml version="1.0" encoding="UTF-8"?>
   <graph caption="0" decimalPrecision="0" formatNumberSacle="0" xAxisName="" yAxisName="">
      <set color="AFD8F8" name="재미있는 강의" value="95"/>
      <set color="F6BD0F" name="자세한 설명" value="43"/>
      <set color="8BBA00" name="요점중심" value="53"/>
   </graph>

 

출처 : http://blog.naver.com/PostView.nhn?blogId=sthwin&logNo=80124184213

 

 

반응형
Comments