<kingeasy>
<property name="Remote.IP" value="61.144.192.91" desc="..."/>
<property name="Remote.Port" value="10000" desc="..."/>
<property name="Remote.Interval" value="10000" desc="..."/>
<property name="Card.Interval" value="1" desc="..."/>
<property name="Upload.Interval" value="10" desc="..."/>
</kingeasy>
如果XML是这种结构,程序应该怎么修改value的值?
想实现这样一个操作,主程序加载时,读取配置文件的信息,并提供一个按钮,跳到修改配置文件的页面,首先将读取出来的配置文件信息赋值到文本框中,修改提交,刚将文本的值替换相应配置文件中的value值,请问程序应该怎么写?

解决方案 »

  1.   

    肯定要使用DOMDocument这个类了
      

  2.   

    要是给你现写太麻烦了,给你个资料看看
    http://www.xuevb.net/modules/news/article.php?storyid=795
      

  3.   

    <Person> 
     <FirstName>Rod</FirstName>
    <LastName>Stephens</LastName>
    </Person>
    可我看的例子差不多都是这种结构的
      

  4.   

    节点查找太麻烦,如果是作为配置文件使用的话,建议用INI。特别是修改时比较麻烦,除非的好的支持XML的第三方控件可以拿来使用
      

  5.   

    '系统属性结构
    Public Type coProperty
        name As String
        value As String
    End Type' 缺省属性
    Private m_colProps As Collection' 当前资源
    Private m_colStrings As Collection'============================================================*
    '方法名:  getProperty
    '版本:    1.0
    '说明:    用于获得系统定义的参数
    '参数:    pID, pDefault
    '返回值:  String
    '============================================================*
    '##ModelId=3CB1B25D032D
    Public Function getProperty(ByVal pID As Variant, Optional ByVal pDefault As String = "") As String
        On Error GoTo errh
        If m_colProps Is Nothing Then
            Call loadProperties
        End If
        getProperty = m_colProps.Item(pID).value
        Exit Function
    errh:
        getProperty = pDefault
    End Function'============================================================*
    '方法名:  loadProperties
    '版本:    1.0
    '说明:    从系统配置中加载属性
    '参数:    pConfigFile
    '============================================================*
    Public Sub loadProperties(Optional ByVal pConfigFile As String)
        On Error GoTo errh
        
        Dim xmlDoc As MSXML2.FreeThreadedDOMDocument
        Dim ElemList
        Dim i
        Dim msg As String
        Dim sPropFile As String * 250
        
        Set m_colProps = New Collection
        
        Set xmlDoc = New MSXML2.FreeThreadedDOMDocument
        If Len(Trim(pConfigFile)) = 0 Then
            sPropFile = App.Path + "\" + "kingeasy.xml"
        Else
            sPropFile = Trim(pConfigFile)
            If InStr(1, sPropFile, "\") = 0 Then
                sPropFile = App.Path + "\" + sPropFile
            End If
        End If
        
        If Not xmlDoc.Load(sPropFile) Then
            With xmlDoc.parseError
                msg = "code:" & .errorCode & _
                    Chr(10) & "Line:" & .Line & _
                    Chr(10) & "Cause:" & .reason & _
                    Chr(10) & "Desc:" & .srcText
            End With
        End If
        
        Dim prop As coProperty
        
        Set ElemList = xmlDoc.getElementsByTagName("property")
        For i = 0 To (ElemList.length - 1)
            prop.name = ElemList.Item(i).Attributes.Item(0).Text
            prop.value = ElemList.Item(i).Attributes.Item(1).Text
            m_colProps.Add prop, prop.name
        Next
        
        xmlDoc.Abort
        Set xmlDoc = Nothing
        Exit Sub
    errh:
        xmlDoc.Abort
        Set m_colProps = Nothing
        Set xmlDoc = Nothing
    End Sub'============================================================*
    '方法名:  setProperty
    '版本:    1.0
    '说明:    设置系统属性
    '参数:    pPropertyName, pPropertyValue
    '返回值:  boolean
    '============================================================*
    Public Sub setProperty(ByVal pPropertyName As String, ByVal pPropertyValue As String)
        On Error GoTo errh
        If m_colProps Is Nothing Then
            Call loadProperties
        End If
        m_colProps.Item(pPropertyName).value = pPropertyValue
        Exit Sub
    errh:
        ' property name not found
        Dim prop As coProperty
        prop.name = pPropertyName
        prop.value = pPropertyValue
        m_colProps.Add prop, prop.name
        Resume Next
    End Sub'============================================================*
    '方法名:  saveProperties
    '版本:    1.0
    '说明:    保存属性到系统配置中
    '参数:    pConfigFile
    '返回值:  boolean
    '============================================================*
    Public Function saveProperties(Optional ByVal pConfigFile As String) As Boolean
        On Error GoTo errh
        
        Dim xmlDoc As MSXML2.FreeThreadedDOMDocument
        Dim ElemList
        Dim msg As String
        Dim sPropFile As String * 250
        
        ' 检查参数
        If Len(Trim(pConfigFile)) = 0 Then
            sPropFile = App.Path + "\" + "kingeasy.xml"
        Else
            sPropFile = Trim(pConfigFile)
            If InStr(1, sPropFile, "\") = 0 Then
                sPropFile = App.Path + "\" + sPropFile
            End If
        End If
        
        If m_colProps Is Nothing Then
            saveProperties = False
            Exit Function
        End If
        
        Set xmlDoc = New MSXML2.FreeThreadedDOMDocument
        Dim i As Integer
        Dim elem, root
        xmlDoc.async = False
        xmlDoc.preserveWhiteSpace = True
        If Not xmlDoc.Load(sPropFile) Then
            With xmlDoc.parseError
                msg = "code:" & .errorCode & _
                    Chr(10) & "Line:" & .Line & _
                    Chr(10) & "Cause:" & .reason & _
                    Chr(10) & "Desc:" & .srcText
            End With
        End If
        
        Set root = xmlDoc.documentElement
        Set elem = xmlDoc.createDocumentFragment
        root.insertBefore elem, root.firstChild
        
        For i = 1 To m_colProps.Count
            Set elem = xmlDoc.selectSingleNode("cyberoffice/property[@name = """ & m_colProps.Item(i).name & """]")
            If elem Is Nothing Then
                Set elem = xmlDoc.createElement("property")
                Call elem.setAttribute("name", m_colProps.Item(i).name)
                Call elem.setAttribute("value", m_colProps.Item(i).value)
                root.appendChild elem
            Else
                Call elem.setAttribute("value", m_colProps.Item(i).value)
            End If
        Next
            
        xmlDoc.Save sPropFile
        Set xmlDoc = Nothing
        saveProperties = True
        Exit Function
    errh:
        xmlDoc.Abort
        Set xmlDoc = Nothing
        saveProperties = False
    End Function发段实例,大家讨论下,学习学习
      

  6.   

    Set elem = xmlDoc.selectSingleNode("cyberoffice/property[@name = """ & m_colProps.Item(i).name & """]")
    请问这句话是啥意思啊,没看明白
      

  7.   

    这句话的意思就是得到一个节点,这个节点cyberoffice节点下的property节点,然后后面中括号内是查询条件,这个节点有个“name”属性,要查询这个属性值=m_colProps.Item(i).name 的那个节点。
      

  8.   

    比如你查找你那个文件的前两个property节点,语句如下Set elem = xmlDoc.selectSingleNode("kingeasy/property[@name = ""Remote.IP""]")
    Set elem = xmlDoc.selectSingleNode("kingeasy/property[@name = ""Remote.Port""]")
      

  9.   

    在调用setProperty时
    m_colProps.Item(pPropertyName).value = pPropertyValue
    并没有把pPropertyValue保存到配置文件中去,请问我应该怎么将传递过来的参数保存到保存到配置文件中去?
    可不可以把saveProperties方法中这段
        Set root = xmlDoc.documentElement
        Set elem = xmlDoc.createDocumentFragment
        root.insertBefore elem, root.firstChild
        
        For i = 1 To m_colProps.Count
            Set elem = xmlDoc.selectSingleNode("cyberoffice/property[@name = """ & m_colProps.Item(i).name & """]")
            If elem Is Nothing Then
                Set elem = xmlDoc.createElement("property")
                Call elem.setAttribute("name", m_colProps.Item(i).name)
                Call elem.setAttribute("value", m_colProps.Item(i).value)
                root.appendChild elem
            Else
                Call elem.setAttribute("value", m_colProps.Item(i).value)
            End If
        Next
    放到setProperty中去,请问应该如何做啊,大哥帮帮忙啊
      

  10.   

    不好意思,上午太忙了
     m_colProps 只是一个 Collection类型的对象,当然没保存到配置文件中。
    你的代码中的流程是根据m_colProps
    重新生成一个新的DOM结构,然后把这个结构保存到一个文件,你这样做也是可以的,最后保存到原来的文件中就可以了。
      

  11.   

    ' 返回节点的值
    Public 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
    '从XML文档中取值.
    Public Sub LoadValues()    Dim xml_document As DOMDocument
        Dim values_node As IXMLDOMNode
        Dim i, m As Integer
        
        ' Load the document.
        Set xml_document = New DOMDocument
        
        ' 取得文件的路径
        m_AppPath = App.Path
        If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"
        xml_document.Load m_AppPath & "Values.xml"
        
        ' 如果LOAD的文件不存在,Do Nothing
        If xml_document.documentElement Is Nothing Then Exit Sub
        
        ' 找到值的集合
        Set values_node = xml_document.selectSingleNode("Values")
        
        '读出保存的数据值
            
        Form1.Text1.Text = GetNodeValue(values_node, "Remote.IP", "")
        Form1.Text2.Text = GetNodeValue(values_node, "Remote.Port", "")
        Form1.Text3.Text = GetNodeValue(values_node, "Remote.Interval", "")      'timer2.interval
    End Sub