2008年8月10日星期日

利用VB6操作XML数据2

SaveValues 程序

   现在我们可以使用XML创建一个简单的程序(如图1),其值存储到XML文件中,在程序开始运行时,程序从VALUE.XML文件中加载数据,在程序运行结束时,将程序中的现行值存入VALUE.XML文件中。



   下面的代码是显示了VALUE.XML文件的结构:

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

   List1显示了怎样编写SaveValues,当载入表单时,form_load事件触发LoadValues子程序。

   LoadValues创建了一个名为xml_document的DOMDocument对象,然后载入xml文件,使用selectSingleNode方法查找名为values的节点,然后使用GetNodeValue方法获得从value节点后代中得到的值。

   GetNodeValue使用value节点的selectSingleNode方法寻找目标节点,如果节点不存在函数将返回一个缺省值,如果找到这个节点GetNodeValue将返回该节点的text值。对于value.xml文件中的数据节点,text仅仅是包含在节点中的文本内容。

   当窗体卸载时触发form_unload事件,unload事件调用SaveValues子程序。程序创建一个新的DOMDocument对象,该对象创建一个新的名为value的节点,然后用文档的appendChild方法将节点添加到文档中。

   在创建所有新的节点后,SaveValues调用DOMDocument's save方法存储新的xml文件。

   注意这个新的文件已经覆盖了旧文件,使用DOMDocument对象无法部分改变XML文件,可以加载XML文件,然后修改其中一部分,然后保存文件,但原文件将被完全覆盖。这是一个小的缺陷,但在这时可以使用其它程序进行修改。

   List1的最后一部分是CreateNode子程序,CreateNode 为父节点创建一个新节点并同时给这个节点赋值。在这个子程序中首先引用一个DOMDocument对象,然后使用该对象的createElement方法创建一个新的节点。

   createNode方法设置节点的text属性,然后将节点作为子节点添加到父节点中。

   List1:

Option Explicit

Private m_AppPath As String

Private Sub Form_Load()
' Get the application's startup path.
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"

' Load the values.
LoadValues
End Sub

Private Sub Form_Unload(Cancel As Integer)
' Save the current values.
SaveValues
End Sub

' Load saved values from XML.
Private Sub LoadValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode

' Load the document.
Set xml_document = New DOMDocument
xml_document.Load m_AppPath & "Values.xml"

' If the file doesn't exist, then
' xml_document.documentElement is Nothing.
If xml_document.documentElement Is Nothing Then
' The file doesn't exist. Do nothing.
Exit Sub
End If

' Find the Values section.
Set values_node = xml_document.selectSingleNode("Values")

' Read the saved values.
txtFirstName.Text = GetNodeValue(values_node, "FirstName", "???")
txtLastName.Text = GetNodeValue(values_node, "LastName", "???")
txtStreet.Text = GetNodeValue(values_node, "Street", "???")
txtCity.Text = GetNodeValue(values_node, "City", "???")
txtState.Text = GetNodeValue(values_node, "State", "???")
txtZip.Text = GetNodeValue(values_node, "Zip", "???")
End Sub

' Return the node's value.
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNode

Set value_node = start_at_node.selectSingleNode(".//" & node_name)
If value_node Is Nothing Then
GetNodeValue = default_value
Else
GetNodeValue = value_node.Text
End If
End Function

' 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 the Values section node to the document.
xml_document.appendChild values_node

' Create nodes for the values inside the
' Values section node.
CreateNode values_node, "FirstName", txtFirstName.Text
CreateNode values_node, "LastName", txtLastName.Text
CreateNode values_node, "Street", txtStreet.Text
CreateNode values_node, "City", txtCity.Text
CreateNode values_node, "State", txtState.Text
CreateNode 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 parent As IXMLDOMNode, _
ByVal node_name As String, ByVal node_value As String)
Dim new_node As IXMLDOMNode

' 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
End Sub

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文件

没有评论: