[讨论]VB读取xml元素遇到了问题
小虾,最近在做个题目为,从VB中读取xml文件中的元素,已经做的差不多了,但遇到个问题,例如: <cj> <ab> <k1>2378</k1> <k1>7845</k1> <k1>8989</k1> </ab> </cj> 只能读取第一个k1标记里的数据,无法读取下面的请,帮我修改下,大侠,小虾感激不禁! 我的vb程序如下: Option Explicit Private p_AppPath As String Private Sub Form_Load() ' 获得程序运行目录 p_AppPath = App.Path If Right$(p_AppPath, 1) <> "\" Then p_AppPath = p_AppPath & "\" ' 加载值 LoadValues End Sub Private Sub Form_Unload(Cancel As Integer) ' 保存现有的值 SaveValues End Sub Private Sub LoadValues() Dim xml_document As DOMDocument Dim values_node As IXMLDOMNode ' 载入文件 Set xml_document = New DOMDocument xml_document.Load p_AppPath & "Online00.xml" If xml_document.documentElement Is Nothing Then Exit Sub End If '寻找节点 Set values_node = xml_document.selectSingleNode("ab") ' 读取各个节点的值 txtName.Text = GetNodeValue(values_node, "k1", "???") txtName.Text = GetNodeValue(values_node, "k1", "???") txtName.Text = GetNodeValue(values_node, "k1", "???") End Sub ' 返回各个节点的值 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 ' 保存现有的值 Private Sub SaveValues() Dim xml_document As DOMDocument Dim values_node As IXMLDOMNode ' 建立XML文件 Set xml_document = New DOMDocument Set values_node = xml_document.createElement("ab") xml_document.appendChild values_node CreateNode values_node, "k1", txtName.Text CreateNode values_node, "k1", txtSex.Text ' 保存XML文件 xml_document.save p_AppPath & "Online00.xml" End Sub Private Sub CreateNode(ByVal parent As IXMLDOMNode, _ ByVal node_name As String, ByVal node_value As String) Dim new_node As IXMLDOMNode Set new_node = parent.ownerDocument.createElement(node_name) new_node.Text = node_value parent.appendChild new_node End Sub |