일반 폼으로 XML 데이터 전송 (JSP+Javascript)

황제낙엽 2005.12.04 09:41 조회 수 : 459 추천:148

sitelink1  
sitelink2  
sitelink3  
sitelink4  
extra_vars4  
extra_vars5  
extra_vars6  
Client Side: JavaScript 에서 XML 노드에 관련된 처리(노드 삽입/삭제/추가)를 수행한다. POST메소드를 이용해서 XML의 모든 내용을 서버에 전송한다.
Server Side: 전송된 XML문서를 저장하거나 그 외에 (Soap 통신등)작업을 수행한다.
관련기술. MSXML : (MSDN Library) - XML Web Services - XML Core - MSXML - XML - XML Reference

기타 XML 기술. 처리 순서
1. HTML, JSP 페이지에서 XML노드 작업을 한다.
2. 히든 필드로 XML을 넣는다.
3. 폼을 전송한다.
4. XML 파일을 파싱한다.

참조 파일 xml.html(클라이언트 측 파일) xml.jsp (서버측 파일)

[xml.html]

<html>
<head>
<script language="javascript">
// 지금은 노드 작업 없이 xml 내용만 전달한다.
// 실제 xml 노드 작업은 관련 메소드를 이용해서 수행 할 수 있다.
function readIt() {
var doc = document.all.SAMPLE_XML.documentElement;
alert(doc.text);
alert(doc.xml);
// <?xml version="1.0" encoding="euc-kr"?> 는 doc.xml 속성에 포함되지 않는다.
document.xml_form.xml_content.value=doc.xml;
return true;
}
</script>
</head>
<body>
<XML ID="SAMPLE_XML">
<?xml version="1.0" encoding="euc-kr"?>
<BOOK category="fiction">
<TITLE> Jonathan </TITLE>
<AUTHOR> 김영규 </AUTHOR> </BOOK> </XML>
<input type="button" value="read xml" omclick="javascript:readIt()"/>
<form name="xml_form" action="xml.jsp" method="post" omSubmit="return readIt();">
  <input type="hidden" name="xml_content"/>
  <input type="hidden" name="encoding" value="euc-kr"/>
  <input type="submit"/>
</form>
</body>
</html>




[xml.jsp]

<%@ page contentType="text/html; charset=euc-kr" %>
<%@ page import="java.io.*"%>
<%@ page import="org.w3c.dom.*"%>
<%@ page import="javax.xml.parsers.*"%>
<%
String xmlContent = request.getParameter("xml_content");
String encoding = request.getParameter("encoding");
System.out.println("xml content");
xmlContent = new String(xmlContent.getBytes("8859_1"), "KSC5601");
StringBuffer content = new StringBuffer();
// <?xml version="1.0" encoding="euc-kr"?> 은 전달되지 않는다.
// 한글을 사용하지 않았을 경우에는 이런 작업은 생략할 수 있다.
content.append("<?xml version="1.0" encoding="").append(encoding).append(""?>");
content.append(xmlContent);
xmlContent = content.toString();
System.out.println(xmlContent);
String author = "";
String title = "";
ByteArrayInputStream in = new ByteArrayInputStream(xmlContent.getBytes("KSC5601"));
try {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(in);
  Element rootEl = doc.getDocumentElement();
  NodeList list = rootEl.getElementsByTagName("AUTHOR");
  if(list.getLength() > 0) {
   Element authorEl = (Element) list.item(0);
   author = authorEl.getFirstChild().getNodeValue();
   System.out.println(author);
  }
  list = rootEl.getElementsByTagName("TITLE");
  if(list.getLength() > 0) {
   Element titleEl = (Element) list.item(0);
   title = titleEl.getFirstChild().getNodeValue();
   System.out.println(title);
  }
} catch (Exception ex) {
  ex.printStackTrace();
}
%>
<html>
<body>
<table>
  <tr>
    <td> Title </td>
    <td><%= title %> </td>
  </tr>
  <tr>
    <td> Author </td>
    <td><%= author %> </td>
  </tr>
</table>
</body>
</html>
번호 제목 글쓴이 날짜 조회 수
246 브라우저의 스크롤을 따라다니는 레이어 두번째 file 황제낙엽 2002.12.20 542
245 마우스 오버시 살짝 뒤로 물러나는 듯한 링크 -_-;; 황제낙엽 2003.01.04 469
244 XP 에서 input type=text 와 input type=password 의 사이즈가 틀리게 보일때 황제낙엽 2004.08.04 517
243 아이디 생성 조건 검사 자바스크립트 모듈 황제낙엽 2004.11.18 478
242 자바스크립트 내장 함수 활용하기 황제낙엽 2005.04.25 518
241 팝업창을 다시 띄우지 않는 소스 황제낙엽 2005.07.16 533
240 무지개링크 (rainbowlink) file 황제낙엽 2005.07.16 500
239 유용한 자바스크립트 예제 몇가지 (Tree 및...) file 황제낙엽 2005.10.20 455
238 브라우저에서 뒤로 가기 막기와 펑션키(function key) 막기 황제낙엽 2005.10.21 579
237 카페의 회람 . 막기 소스 황제낙엽 2005.10.21 240
236 소스 보기 막기 황제낙엽 2005.11.18 540
235 JAVASCRIPT Debuger 프로그램 file 황제낙엽 2005.11.22 342
234 JAVASCRIPT REFERENCE 파일 file 황제낙엽 2005.11.22 460
233 자바 스크립트 플러그인 황제낙엽 2005.11.22 425
232 풍선 도움말 황제낙엽 2005.11.24 326
231 XML+JS 연동 다중셀렉트박스 (1) - <font color="brown">(MS Explorer 전용)</brown> 황제낙엽 2005.12.02 390
230 슬라이딩 메뉴 황제낙엽 2005.12.02 491
229 Methods and properties of Microsoft.XMLDOM 황제낙엽 2005.12.04 427
» 폼으로 XML 데이터 전송 (JSP+Javascript) 황제낙엽 2005.12.04 459
227 자바스크립트로 서버의 XML파일을 접근 (실패했슴) 황제낙엽 2005.12.11 715