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本身就很规范,编写这个规格转换器所以很简单的!
' 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