<Addresses> <Entry Type="Personal"> <FirstName>Andy</FirstName> <LastName>Fickle</LastName> <Street>1234 Programmer Place</Street> <City>Bugsville</City> <State>CO</State> <Zip>82379</Zip> <Phone Type="Home">354-493-9489</Phone> </Entry> <Entry Type="Work"> <FirstName>Betty</FirstName> <LastName>Masterson</LastName> <Phone Type="Work">937-878-4958</Phone> <Phone Type="WorkFax">937-878-4900</Phone> </Entry> ... </Addresses> |
Dim xml_document As New DOMDocument xml_document.loadXML _ "<Person>" & vbCrLf & _ " <FirstName>Rod</FirstName>" & vbCrLf & _ " <LastName>Stephens</LastName>" & vbCrLf & _ "</Person>" |
@# Search for a child node named "LastName." Set last_name_node = address_node.selectSingleNode("LastName") @# Search for any descendant named "LastName." Set last_name_node = address_node.selectSingleNode(".//LastName") |
@# 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 @# 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 SaveValues 程序 |
<Values> <FirstName>Rod</FirstName> <LastName>Stephens</LastName> <Street>1234 Programmer Place</Street> <City>Bugsville</City> <State>CO</State> <Zip>80276</Zip> </Values> |
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 |
<Values><FirstName>Rod</FirstName><LastName>Stephens</LastNa me><Street>1234 Programmer Place</Street><City>Bugsville</Ci ty><State>CO</State><Zip>80276</Zip></Values> |
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 |