DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XML information class Version 1.0
XML information class Version 1.0
編輯:XML詳解     

<%
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''
''''  XML information Class Version 1.0
''''
''''  Author: Xinsoft [ Xin Yaping ]
''''  Email:  XinsoftATk65.Net
''''  Xinsoft , 2005-04-28 Xinsoft DevStudio
''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
%><%

Class XMLInfo

   Public encoding     '' as String
  
   Public NodeCount
   Public NodeArray
  
   Private XMLURL      '' as String
   Private xmlDoc      '' as MsXML2.DOMDocument.4.0
  
   Private ReadyState  '' as Integer
   Private Status      '' as Integer
   Private LoadSuccess '' as Bool
   Private parseError  '' as Integer
  
   Private curSingleNode   '' as Node
   Private curNodeList     '' as Node List
   Private curXpath        '' as XPath


Private Sub Class_Initialize
   XMLURL=""
   encoding="GB2312"
   NodeCount=0
  
   ReadyState=0
   Status=0
   LoadSuccess=False
   parseError=0
  
   Set xmlDoc = CreateObject("MsXML2.DOMDocument.4.0")
End Sub
Private Sub Class_Terminate
   Set XMLDoc = Nothing
  
   If True = IsObject(NodeArray) Then
       Set NodeArray = Nothing
   End If
   If True = IsObject(curSingleNode) Then
       Set curSingleNode = Nothing
   End If
   If True = IsObject(curNodeList) Then
       Set curNodeList = Nothing
   End if
End Sub

Function BytesToBstr(strBody,CodeBase)
       dim obJStream
       Set obJStream = Server.CreateObject("Adodb.Stream")
       obJStream.Type = 1
       obJStream.Mode = 3
       obJStream.Open
       obJStream.Write strBody
       obJStream.Position = 0
       obJStream.Type = 2
       obJStream.Charset = CodeBase
       BytesToBstr = obJStream.ReadText
       obJStream.Close
       Set obJStream = Nothing
End Function

Public Function httpGet()
    
     Dim Cont
     Dim objXMLHttp
    
     Set objXMLHttp=Server.CreateObject("Msxml2.XMLHTTP")
    
     objXMLHttp.open "GET",XMLURL,False
     objXMLHttp.send()
    
     ReadyState = objXMLHttp.readyState
     Status = objXMLHttp.status
    
     If 4=ReadyState And 200=Status Then
           Cont=objXMLHttp.responseBody
     Else
           Cont=""
     End If
    
     Set objXMLHttp=Nothing
    
     Cont=BytesToBstr(Cont,encoding)
    
     httpGet=Cont
End Function

Public Sub PrintParseErrorInfo()
   If parseError = 1 Then
       
       Response.Write "<P>Some error found in your XML document.</P>"
       
       Dim xPE
       Set xPE = XMLDoc.parseError
       
       Response.Write "<p>XMLDoc.parseError.reason = "& xPE.reason &"</p>"
       Response.Write "<p>XMLDoc.parseError.filepos = "& xPE.filepos &"</p>"
       Response.Write "<p>XMLDoc.parseError.line = "& xPE.line &"</p>"
       Response.Write "<p>XMLDoc.parseError.linepos = "& xPE.linepos &"</p>"
       Response.Write "<p>XMLDoc.parseError.srcText = "& xPE.srcText &"</p>"
       Response.Write "<p>XMLDoc.parseError.URL = "& xPE.URL &"</p>"
       
       Set xPE = Nothing
       
   Else
       Response.Write("")
   End if
End Sub

Property Let url( XMLSourceHttp )

   Dim Cont
  
   xmlURL=XMLSourceHttp
  
   XMLDoc.async = False
   XMLDoc.validateOnParse = False
  
   Cont = httpGet()
   LoadSuccess = xmlDoc.LoadXML(Cont)
   ReadyState = XMLDoc.ReadyState
  
   If XMLDoc.parseError.errorCode <> 0 Then
       parseError = 1
   Else
       parseError = 0
   End If
  
   ''//If True = LoadSuccess Then
   ''//End If

End Property

Property Get url()
   url=XMLURL
End Property

Property Let Xpath( path )
   curXpath=path
End Property
Property Get Xpath()
   Xpath=curXpath
End Property

Property Let SingleNode( Xpath )
   curXpath=Xpath
   Set curSingleNode=XMLDoc.documentElement.selectSingleNode( curXpath )
End Property
Property Get SingleNode()
   Set SingleNode=curSingleNode
End Property

Property Let NodeList( Xpath )
   Dim i
  
   curXpath=Xpath
   Set curNodeList=XMLDoc.documentElement.selectNodes( curXpath )
  
   NodeCount=curNodeList.length
   ReDim NodeArray(NodeCount)
   For i=0 To NodeCount-1
       Set NodeArray(i)=curNodeList.item(i)
   Next
  
End Property
Property Get NodeList()
   Set NodeList=curNodeList
End Property

Property Get loadStat()
   loadStat=LoadSuccess
End Property

Public Function getChildNodeValByName( objNode , ChiidNodeName )
   getChildNodeValByName=objNode.selectSingleNode(ChiidNodeName).text
End Function

Property Get ChildValInList( index , ChildName )
   ChildValInList=getChildNodeValByName( NodeArray(index) , ChildName )
End Property

Property Get ChildVal( ChildName )
   ChildVal=getChildNodeValByName( curSingleNode , ChildName )
End Property

Property Get NodeVal( Xpath )
   NodeVal=XMLDoc.documentElement.selectSingleNode( Xpath ).text
End Property

Property Get XpathNodesLength( Xpath )
   XpathNodesLength=XMLDoc.documentElement.selectNodes(Xpath).length
End Property

Property Get NodeValInList( Xpath , index )
   NodeValInList=XMLDoc.documentElement.selectNodes(Xpath).item(index).text
End Property

End Class

%>


XML information Class Version 1.0 調用示范

<% option explicit %>
<!-- #include file="../../lib/XML.ASP" -->
<%
Dim Str,i,N

Dim objXML
Set objXML = New XMLInfo

With objXML
   .url="http://210.77.146.223/tech/indexXML.PHP";
   .encoding="gb2312"
End With

objXML.NodeList="/blogtech/item"
Response.Write "<p>objXML.RecN="&objXML.NodeCount&"</p>" & vbCrLf

For i=0 To objXML.NodeCount-1
   Response.Write "<p>"&objXML.ChildValInList(i,"title")&"</p>" & vbCrLf
Next

Response.Write "<hr />Test SingleNode<hr />" & vbCrLf

objXML.SingleNode="/blogtech"
Response.Write "objXML.ChildVal(""description"")="& objXML.ChildVal("description") &"" & vbCrLf

Response.Write "<hr />Test NodeVal<hr />" & vbCrLf
Response.Write "objXML.NodeVal(""/blogtech/description"")=" & objXML.NodeVal("/blogtech/description") & vbCrLf
Response.Write "<hr />Test XpathNodesLength and NodeValInList<hr />" & vbCrLf

N=objXML.XpathNodesLength("/blogtech/item/link")
Response.Write "<p>objXML.XpathNodesLength(""/blogtech/item/link"")="&N&"</p>"
For i=0 To N-1
   Response.Write "<p>"& objXML.NodeValInList("/blogtech/item/link",i) &"</p>" & vbCrLf
Next

Set objXML = Nothing

%>


XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved