科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件在Visual Basic 6.0中操纵XML文件

在Visual Basic 6.0中操纵XML文件

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立。

作者:crystal编译 来源:yesky 2007年10月15日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
SaveValuesIndented 程序

  虽然每个人都化了很大的精力去处理xml文档,使他们看上更容易些,但xml工具一般都忽略了那些使xml文档结构明显的空白和缩进,xml解析器也同样忽略缩进和空白。

  不幸的是我们例子也同样忽略了这些缩进和空白,SaveValues创建了一个象下面那样的xml文件,所有的代码都在同一行中。

<Values><FirstName>Rod</FirstName><LastName>Stephens</LastNa
me><Street>1234 Programmer Place</Street><City>Bugsville</Ci
ty><State>CO</State><Zip>80276</Zip></Values>

  VB.NET中包括了文本写入类,可以XML文档规定格式。但MSXML重没有这种功能,所以如果需要以一种清晰的格式保存XML文件,只能另行添加它的格式。

  List2列出了程序SaveValuesIndented使用的代码,SaveValues子程序与上面例子中讲的几乎完全相同,但他在创建value节点后同时给XML文档创建了一个<value>标记的新行。

  然后SaveValues 调用CreateNode创建一个新的数据节点,但在这里它传递给CreateNode一个新的参数,这个参数表示这个新节点的缩进方式。

CreateNode
' Save the current values.
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode

' Create the XML document.
Set xml_document = New DOMDocument

' Create the Values section node.
Set values_node = xml_document.createElement("Values")

' Add a new line.
values_node.appendChild xml_document.createTextNode(vbCrLf)

' Add the Values section node to the document.
xml_document.appendChild values_node

' Create nodes for the values inside the
' Values section node.
CreateNode 4, values_node, "FirstName", txtFirstName.Text
CreateNode 4, values_node, "LastName", txtLastName.Text
CreateNode 4, values_node, "Street", txtStreet.Text
CreateNode 4, values_node, "City", txtCity.Text
CreateNode 4, values_node, "State", txtState.Text
CreateNode 4, values_node, "Zip", txtZip.Text

' Save the XML document.
xml_document.save m_AppPath & "Values.xml"
End Sub

' Add a new node to the indicated parent node.
Private Sub CreateNode(ByVal indent As Integer, _
ByVal parent As IXMLDOMNode, ByVal node_name As String, _
ByVal node_value As String)
Dim new_node As IXMLDOMNode

' Indent.
parent.appendChild _
parent.ownerDocument.createTextNode(Space$(indent))

' Create the new node.
Set new_node = parent.ownerDocument.createElement(node_name)

' Set the node's text value.
new_node.Text = node_value

' Add the node to the parent.
parent.appendChild new_node

' Add a new line.
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
End Sub

  结论

  本文仅仅揭示XML编程的表面,本文的例子中的涉及只是非常简单的XML文件,但你可以使用使用本文揭示的技术做更多的事情,比如配置设置、表单位置、以及其他信息。XML已经向前更进一步的发展了,有了更复杂的数据层次。对于更复杂的数据结构,在运行时可以更容易的使用MSXML对象来存取XML文件

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章