VB6.0操作XML文件 
XML文件如下:
<?xml version="1.0" encoding="gb2312"?>
<testlist>
 <test>
  <id>1</id>
  <name>自由飞翔</name>
  <time>2007-12-7</time>
 </test>
<test>
  <id>2</id>
  <name>自由</name>
  <time>2007-12-7</time>
 </test>
</testlist>
 
以下是VB6.0的代码:
'工程->引用->Mircosoft XML6.0
Dim XMLDoc As DOMDocument
Dim root As IXMLDOMNode
Dim xlst As IXMLDOMNodeList, xlst1 As IXMLDOMNodeList
Dim xn As IXMLDOMNode
Dim xnf As IXMLDOMNode
Dim xe As IXMLDOMElement
Set XMLDoc = New DOMDocument
XMLDoc.Load "F:\l"  'xml文件路径根据存放路径不同而不同!
Set xn = XMLDoc.selectSingleNode("testlist")
'以下为读取xml文件内容,这里用ListView控件显示
Set xlst = xn.childNodes
ListView1.ListItems.Clear
For Each xnf In xlst
        Set xe = xnf
        Set xlst1 = xe.childNodes
        With ListView1.ListItems.Add(, , xlst1.Item(0).Text)
            .SubItems(1) = xlst1.Item(1).Text
            .SubItems(2) = xlst1.Item(2).Text
        End With
Next
 
'以下是插入一个节点(这里插入一个id=3,name=飞翔,time=date 的节点)
Dim XMLId As IXMLDOMElement
Dim XMLName As IXMLDOMElement
Dim XMLTime As IXMLDOMElement
Dim returnValue As IXMLDOMNode
Set xe = ateElement("test") '创建一个<test>节点
Set XMLId = ateElement("id")
XMLId.Text = 3
Set returnValue = xe.appendChild(XMLId)
Set XMLName = ateElement("name")
XMLName.Text = "飞翔"
Set returnValue = xe.appendChild(XMLName)
Set XMLTime = ateElement("time")
XMLTime.Text = Datevb listview控件
Set returnValue = xe.appendChild(XMLTime)
Set returnValue = xn.appendChild(xe) '添加到<testlist>节点中
XMLDoc.Save "F:\l"
 
'删除XML中id=2的节点
Set xlst = XMLDoc.SelectSingleNode("testlist").ChildNodes
For Each xnf In xlst
        Set xe = xnf
        If xe.childNodes(0).Text = "2" Then
            xn.removeChild xe
        End If
Next xnf
XMLDoc.Save  "F:\l"

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。