如何通过VB处理XML

xiaoxiao2021-03-01  3

1. 首先要引用一个Microsoft XML 随便选个版本。 2. 在初始化的时候读取XML显示到TXTBOX中,代码如下: '通过2进制流将XML文件读出来,也可以是其它文件格式 Private Function pstrUpdateView(istrXMLPath As String) As String Dim wlngFreeFile As Long Dim wbytwbytLoadBytes() As Byte '获得一个空闲文件号 wlngFreeFile = FreeFile Open istrXMLPath For Binary As #wlngFreeFile ReDim wbytLoadBytes(1 To LOF(wlngFreeFile)) As Byte Get #wlngFreeFile, , wbytLoadBytes Close wlngFreeFile pstrUpdateView = StrConv(wbytLoadBytes, vbUnicode) End Function 3. 装载XML。传进去一个XML地址,如果装载不成功就error,成功则执行下一步 Private Sub fsubLoadXML(istrXMLPath As String) Set pobjXMLDoc = CreateObject("MSXML2.DOMDocument") If pobjXMLDoc.Load(istrXMLPath) = False Then On Error GoTo LoadXMLErr: End If On Error GoTo 0 Exit Sub LoadXMLErr: Dim myErr Set myErr = pobjXMLDoc.parseError MsgBox ("ERROR:" & myErr.reason) Set myErr = Nothing End Sub 4. 读属性。DOMDocument对象里有2个读节点的方法: 1. selectNodes() 如果根节点下有多个子节点就要用这个方法,item定义了第几个子节点 2. selectSingleNode()如果根节点下只有一个字节点可以用这个方法 1. Private Function fstrReadAttr(istrNodes As String, istrAttribute As String) As String On Error GoTo ErrHandle: Dim wobjXmlAttr As MSXML2.IXMLDOMAttribute ' ' Set wobjXmlAttr = pobjXMLDoc.selectNodes(istrNodes).Item(0).Attributes.getNamedItem(istrAttribute) ' Set wobjXmlAttr = pobjXMLDoc.selectSingleNode(istrNodes).Attributes.getNamedItem(istrAttribute) fstrReadAttr = wobjXmlAttr.Text 'destroy object Set wobjXmlAttr = Nothing On Error GoTo 0 Exit Function ErrHandle: MsgBox Err.Description Set wobjXmlAttr = Nothing End Function 5. 读节点。 call fstrReadNode("/test/user") ,参数是test节点下的user子节点 Private Function fstrReadNode(istrNodes As String) As String Dim xNode As MSXML2.IXMLDOMNode Set xNode = pobjXMLDoc.selectSingleNode(istrNodes) fstrReadNode = xNode.Text Set xNode = Nothing End Function 6. 写节点。参数1:节点;参数2:需要写入的值 Private Sub fsubWriteNode(istrNodes As String, istrValue As String) Dim wobjXMLNode As IXMLDOMElement Set wobjXMLNode = pobjXMLDoc.documentElement.selectNodes(istrNodes).Item(0) wobjXMLNode.Text = istrValue Set wobjXMLNode = Nothing End Sub MSXML 处理 xml 文档时外部DTD定义的问题(ASP) 项目中碰到这个问题,所以也贴了出来 xmlfile = "http://myserver/catalog.xml" xslfile = "catalog.xsl" ' 创建相关对象 Set xslDoc = server.CreateObject("MSXML2.FreeThreadedDOMDocument") Set xmlDoc = server.CreateObject("MSXML2.DOMDocument") ' 读取xsl文件 xsldoc.async = False xsldoc.resolveExternals = True xsldoc.load server.MapPath(xslfile) ' 读取xml文件 xmldoc.setProperty "ServerHTTPRequest",True ' 设置ServerHTTPRequest 属性为 True 为了通过http协议载入xml文档 xmldoc.async = False ' 设置 async属性为 False 关闭异步调用 xmldoc.resolveExternals = True ' 设置 resolveExternals 为 True 打开外部DTD分析 xmldoc.validateOnParse = False ' 设置 validateOnParse 为 False 允许文档验证 xmldoc.load xmlfile ' 读取xml文档 Do While (xmldoc.ReadyState < 4) ' 检查ReadyState状态值是否为4 ' 具体数值定义参见msxml sdk document xmldoc.waitForResponse 10 ' 通过waitForResponse方法等待文档完全读取完毕 ' 如果为读取完成,系统暂停10毫秒 Loop ' 转换xml -> html 并输出文档 xmldoc.transformNodeToObject xsldoc,Response ' 清空对象 Set xslt = Nothing Set xsldoc = Nothing Set xmldoc = Nothing 相关资源:使用VB操纵XML文档.pdf
转载请注明原文地址: https://www.6miu.com/read-3850036.html

最新回复(0)