标题:如何加载XML?
只看楼主
panzy0721
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-8-3
结帖率:0
已结贴  问题点数:20 回复次数:1 
如何加载XML?
如何用VB读取XML ,  求告诉用啥控件  然后又怎么远程读取呢
搜索更多相关主题的帖子: 如何 
2016-10-18 17:56
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:364
帖 子:4912
专家分:29900
注 册:2008-10-15
得分:20 
远程,根据你的服务器的方案,选择,
1、winsock
以下三个仅支持服务器使用WEB服务。
2、inte
3、XMLHTTP
4、webbrowser

简单,就是第3种,全能就是第一种。

下面是网上搜索一个模块,使用的是第3种,你参考一下。
程序代码:
'==========================================================
'| 模 块 名 | XMLHTTP
'| 说  明 | 替代Inet控件,实现数据通讯
'==========================================================
Public Enum DataEnum
  ResponseText = 1
  ResponseBody = 2
End Enum

 
Public Function GetData(ByVal Url As String, ByVal DataStic As DataEnum) As Variant
  
  On Error GoTo ERR:
  Dim XMLHTTP As Object
  Dim DataS As String
  Dim DataB() As Byte
  
  Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
  
  XMLHTTP.Open "get", Url, True
  XMLHTTP.send
  
  While XMLHTTP.ReadyState <> 4
    DoEvents
  Wend
  '--------------------------------------函数返回
  Select Case DataStic
  Case ResponseText
    '--------------------------------直接返回字符串
    DataS = XMLHTTP.ResponseText
    GetData = DataS
  Case ResponseBody
    '--------------------------------直接返回二进制
    DataB = XMLHTTP.ResponseBody
    GetData = DataB
  Case ResponseBody + ResponseText
    '------------------------------二进制转字符串[直接返回字串出现乱码时尝试]
    DataS = BytesToStr(XMLHTTP.ResponseBody)
    GetData = DataS
  Case Else
    '--------------------------------无效的返回
    GetData = ""
  End Select
  '--------------------------------------释放空间
  Set XMLHTTP = Nothing
  Exit Function
ERR:
  GetData = ""
End Function

 
Public Function PostData(ByVal StrUrl As String, ByVal StrData As String, ByVal DataStic As DataEnum) As Variant
  On Error GoTo ERR:
  
  Dim XMLHTTP As Object
  Dim DataS As String
  Dim DataB() As Byte
  
  Set XMLHTTP = CreateObject("Microsoft.XMLHTTP")
  
  XMLHTTP.Open "POST", StrUrl, True
  XMLHTTP.setRequestHeader "Content-Length", Len(PostData)
  XMLHTTP.setRequestHeader "CONTENT-TYPE", "application/x-www-form-urlencoded"
  XMLHTTP.send (StrData)
  
  Do Until XMLHTTP.ReadyState = 4
    DoEvents
  Loop
  '-----------------------------函数返回
  Select Case DataStic
  Case ResponseText
    '--------------------------------直接返回字符串
    DataS = XMLHTTP.ResponseText
    PostData = DataS
  Case ResponseBody
    '--------------------------------直接返回二进制
    DataB = XMLHTTP.ResponseBody
    PostData = DataB
  Case ResponseBody + ResponseText
    '---------------------------二进制转字符串[直接返回字串出现乱码时尝试]
    DataS = BytesToStr(XMLHTTP.ResponseBody)
    PostData = DataS
  Case Else
    '--------------------------------无效的返回
    PostData = ""
  End Select
  '------------------------------------释放空间
  Set XMLHTTP = Nothing
  Exit Function
ERR:
  PostData = ""
End Function

 
Function BytesToStr(ByVal vIn) As String
  strReturn = ""
  For i = 1 To LenB(vIn)
    ThisCharCode = AscB(MidB(vIn, i, 1))
    If ThisCharCode < &H80 Then
      strReturn = strReturn & Chr(ThisCharCode)
    Else
      NextCharCode = AscB(MidB(vIn, i + 1, 1))
      strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
      i = i + 1
    End If
  Next
  BytesToStr = strReturn
End Function

授人于鱼,不如授人于渔
早已停用QQ了
2016-10-18 19:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-469946-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.227412 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved