> Hello World !!!

     

@syaku

DB result 결과를 XML로 출력하기 : ASP

 

DB 데이터 조회를 리턴 받아 xml 형식으로 뿌려주는 클래스

 

Result

Source

<% 
'// X-Framework With ASP
'// XmlClasses.asp
'// programmed by 최석균
'// http://www.cyworld.com/syaku
'// http://syaku.tistory.com
 
 
Class XmlClasses
   Private nodeRoot,nodeItem,nodeMessage,nodeError,nodeAct,nodeActRet

   '## 자동처리 ##
   Sub Class_Initialize                   
   End Sub
   Sub Class_Terminate
             Set objXml = Nothing
   End Sub

   Public Function objXml
             '// XMLDOM 생성
             Set objXml = Server.CreateObject("Microsoft.XMLDOM")                
   End Function

   '// 받은 쿼리 데이터를 XML 형식으로 변환
   Public Function XmlQuery(Ar,Field)
    Dim objXml
    Dim nodeTitle,nodeName,nodeValue
    Dim key,title,value
    Dim i,x

    Set objXml = Me.objXml

    objXml.async = False
    objXml.appendChild(objXml.createProcessingInstruction("xml","version=""1.0"" encoding=""" & Application("CharSet") & """ "))

    ' 루트 생성
    Set nodeRoot = objXml.CreateElement("xf_xml")                
    objXml.appendChild(nodeRoot)

    If IsArray(Ar) Then
      For i = 0 To UBound(Ar,2)          
        Set nodeItem = nodeRoot.AppendChild(objXml.CreateElement("item"))         

        x = 0
        For Each key In Split(Field,",")
          Set nodeName = nodeItem.AppendChild(objXml.CreateElement(key))

          value = Ar(x,i)
          If Not IsNull(value) Then
                    nodeName.Text = objXml.createCDATASection(value).text
          End If
          x = x + 1
        Next
      Next
    End If

    Set XmlQuery = objXml
   End Function
            
   '// 모든 에러 메세지 출력
   Public Function XmlError(ErrStr)
    Dim objXml
    Dim textMessage,textError

    textMessage = ""

    Set objXml = Me.objXml

    objXml.async = False
    objXml.appendChild(objXml.createProcessingInstruction("xml","version=""1.0"" encoding=""" & Application("CharSet") & """ "))

    Set nodeRoot = objXml.AppendChild(objXml.CreateElement("xf_xml"))
    Set nodeItem = nodeRoot.AppendChild(objXml.CreateElement("item"))
    Set nodeMessage = nodeItem.AppendChild(objXml.CreateElement("message"))
    Set nodeError = nodeItem.AppendChild(objXml.CreateElement("error"))

    textMessage = "에러 발생" & ErrStr        
    textError = 1

    nodeMessage.Text = objXml.createCDATASection(textMessage).text
    nodeError.Text = textError

    Set XmlError = objXml
   End Function    
End Class
 
' 클래스 호출
Dim XmlCls
Set XmlCls = new XmlClasses
%>

Example Source

<%@LANGUAGE="VBSCRIPT"%>
 
<% Option Explicit %>
 
<%
' 949 : EUC-KR / 65001 : UTF8 (언어셋 선택)
Application("CodePage") = 949
Application("CharSet") = "EUC-KR"
%>
 
<!--#include file=./xmlClasses.asp -->
 
<%
' Xml 형식
Response.ContentType = "text/xml"
 
' 오류 발생될때 무시
On Error Resume Next
 
Dim Sql,Ar
 
' 우편번호 데이터
Sql = " SELECT " &_
        "ZIPCODE, " &_
        "SIDO, " &_
        "GUGUN, " &_
        "DONG, " &_
        "BUNJI " &_
        "FROM xf_zipcode " &_
        "WHERE DONG LIKE '%해도2동%'"
 
' XF 용 클래스 (무시)
ConnCls.LetQuery = Sql
 
' XF 용 클래스 (무시)
' Getrow 배열로 데이터 리턴
Ar = ConnCls.QueryExec("select_getrow")
 
Dim field,XmlAction
 
' SELECT 문의 필드 순서와 똑같아야한다.
' 필등명은 똑같이 않아도 된다.
field = "ZIPCODE,SIDO,GUGUN,DONG,BUNJI"
 
Set XmlAction = XmlCls.XmlQuery(Ar,field)
 
 
'// 오류 발생할 경우 (디버깅용)
If Err.Number <> 0 Then
  Dim xmlStr

  xmlStr = Chr(10) & "[오류 코드 : " & Err.Number & "] " & Chr(10) & Err.Source & Chr(10) & Err.Description & Chr(10) & ConnCls.GetQuery

  Set XmlAction = XmlCls.XmlError(xmlStr)   
End If
 
Err.Clear

'// XML 출력
XmlAction.save response
%>

written by Seok Kyun. Choi. 최석균.
http://www.cyworld.com/syaku
http://syaku.tistory.com