- JSP페이지에서 만들기
- JDOM
/** * @class Xml * @brief Xml Classes * * registered date 20100105 * programmed by Seok Kyun. Choi. 최석균 * http://syaku.tistory.com */ package org.syaku.util; import java.util.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; public class Xml { // 해쉬맵을 이용하여 문서 생성 public Document make(HashMap hm) throws Exception { Element data = new Element("data"); Element element = new Element("item"); Set set = hm.keySet(); Object []items = set.toArray(); for(int i = 0; i < items.length; i++) { String name = (String) items[i]; String value = (String) hm.get(name); addElement(element,name,value); } data.addContent(element); Document document = new Document(data); return document; } // 엘리먼트 생성 public Element addElement(Element parent, String name, String value) { Element element = new Element(name); element.setText(value); parent.addContent(element); return parent; } // 애트리뷰트 생성 public void addAttribute(Element element, String name, String value){ Attribute attribute = new Attribute(name,value); element.setAttribute(attribute); } }
- XML출력
<%@ page contentType="text/html; charset=euc-kr"%> <%@ page import="java.util.*" %> <%@ page import="org.syaku.util.*" %> <%@ page import="org.jdom.*" %> <%@ page import="org.jdom.output.*" %> <% HashMap hm = new HashMap(); hm.put("샤쿠","Syaku"); hm.put("Site","syaku.tistory.com"); hm.put("블로그","Blog"); Xml xml = new Xml(); Document document = (Document) xml.make(hm); XMLOutputter outputter = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setEncoding("EUC-KR"); outputter.setFormat(format); response.setContentType("application/xml"); outputter.output(document, response.getWriter()); // 시스템출력 // outputter.output(document, System.out); %>
- 생성된 XML
댓글 없음:
댓글 쓰기