包括添加节点,删除节点,修改节点.
查询

解决方案 »

  1.   

    添加节点:dim xmlDoc as DomDocument
    dim nodeRoot as IXMLDOMNode
    dim node as IXMLDOMNode    str = "<?xml version=""1.0""?><RootNode/>"
        Set xmlDoc = New DOMDocument
        xmlDoc.loadXML str
        
        Set nodeRoot = xmlDoc.documentElement    Set Node = xmlDoc.createNode(NODE_ELEMENT, "MyItem", "")
        Node.Text = "Test Content"
        nodeRoot.appendChild Node    MsgBox nodeRoot.xml
      

  2.   

    修改节点:比如对于下列XML:
    <?xml version="1.0" encoding="gb2312"?>
    <CoverRows>
       <Row Key="KEY0">
            <Item Name="No" Text="No 0"/>
            <Item Name="Name" Text="Name 0"/>
       </Row>
    <CoverRows>如果要将第一个Item的Text修改成My Content,如下:dim xmlDoc as DomDocument
    dim nodeRoot as IXMLDOMNode
    dim node as IXMLDOMNode    str = "<?xml version=""1.0""?>......."    '上面的字符串
        Set xmlDoc = New DOMDocument
        xmlDoc.loadXML str
        
        Set nodeRoot = xmlDoc.documentElement
        set node=nodeRoot.SelectSingleNode("CoverRows/Row/Item[@Name=""No""]")
        node.Text="My Content"    MsgBox nodeRoot.xml如果要删除第一个Item节点,则set node=nodeRoot.SelectSingleNode("CoverRows/Row[@Key=""KEY0""]")
    node.removeChild node.childnode(0)msgbox nodeRoot.xml
      

  3.   

    XPath查询比较简单,看多几个例子就可以了,这是XML SDK提供的XPath例子:Expression Refers to 
    ./author
     All <author> elements within the current context. Note that this is equivalent to the expression in the next row. 
    author
     All <author> elements within the current context. 
    first.name
     All <first.name> elements within the current context. 
    /bookstore
     The document element (<bookstore>) of this document. 
    //author
     All <author> elements in the document. 
    book[/bookstore/@specialty = @style]
     All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookstore> element at the root of the document. 
    author/first-name
     All <first-name> elements that are children of an <author> element. 
    bookstore//title
     All <title> elements one or more levels deep in the <bookstore> element (arbitrary descendants). Note that this is different from the expression in the next row. 
    bookstore/*/title
     All <title> elements that are grandchildren of <bookstore> elements. 
    bookstore//book/excerpt//emph
     All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookstore> element. 
    .//title
     All <title> elements one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required. 
    author/*
     All elements that are the children of <author> elements. 
    book/*/last-name
     All <last-name> elements that are grandchildren of <book> elements. 
    */*
     All grandchildren elements of the current context. 
    *[@specialty]
     All elements with the specialty attribute. 
    @style
     The style attribute of the current context. 
    price/@exchange
     The exchange attribute on <price> elements within the current context. 
    price/@exchange/total
     Returns an empty node set, because attributes do not contain element children. This expression is allowed by the XML Path Language (XPath) grammar, but is not strictly valid. 
    book[@style]
     All <book> elements with style attributes, of the current context. 
    book/@style
     The style attribute for all <book> elements of the current context. 
    @*
     All attributes of the current element context. 
    ./first-name
     All <first-name> elements in the current context node. Note that this is equivalent to the expression in the next row. 
    first-name
     All <first-name> elements in the current context node. 
    author[1]
     The first <author> element in the current context node. 
    author[first-name][3]
     The third <author> element that has a <first-name> child. 
    my:book
     The <book> element from the my namespace. 
    my:*
     All elements from the my namespace. 
    @my:*
     All attributes from the my namespace (this does not include unqualified attributes on elements from the my namespace). 
      

  4.   

    不好意思,写错了, 修改里面有一行(因为nodeRoot就是CoverRows节点。):
    set node=nodeRoot.SelectSingleNode("CoverRows/Row/Item[@Name=""No""]")
    需要改成:
    set node=nodeRoot.SelectSingleNode("Row/Item[@Name=""No""]")删除里面一样,
    set node=nodeRoot.SelectSingleNode("CoverRows/Row[@Key=""KEY0""]")
    改成:
    set node=nodeRoot.SelectSingleNode("Row[@Key=""KEY0""]")
    这两句也可以进行如下修改:set node=xmldoc.SelectSingleNode("CoverRows/Row/Item[@Name=""No""]")
    set node=xmldoc.SelectSingleNode("CoverRows/Row[@Key=""KEY0""]")
      

  5.   

    Public Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, ByVal node_name As String, ByVal default_value As String) As String  Dim obj_node As IXMLDOMNode
      Set obj_node = start_at_node.selectSingleNode(node_name)
      If obj_node Is Nothing Then
        GetNodeValue = default_value
      Else
        GetNodeValue = obj_node.Text
      End IfEnd Function调用的时候只能这样写GetNodeValue search_node, "username", "a"
    下面这句就不行了,我把s定义成string类型的了
    s=GetNodeValue search_node, "username", "a"
      

  6.   

    晕,为什么要加括号才行
    s = GetNodeValue(search_node, "username", "a")
      

  7.   

    vb里的语句就是这样GetNodeValue search_node, "username", "a"  '正确写法
    GetNodeValue(search_node, "username", "a")  '错误写法s=GetNodeValue (search_node, "username", "a")'正确写法
    s=GetNodeValue search_node, "username", "a"'错误写法
      

  8.   

    dim values_node as ixmldomelement
    dim topTag as String
    'toptag标记是顶层节点
        XmlStr = "<?xml version=""1.0"" encoding=""gb2312""?>"
        xml_document.loadXML XmlStr
        Set values_node = xml_document.documentElement
         
    Set values_node = xml_document.createElement(TopTag)
    xml_document.appendChild values_node结果是没有<?xml version="1.0" encoding="gb2312"?>
      

  9.   

    google search xml dom还有就是跟html的dom相类似主要把element node对象理解,对照着dom手册就可以了
      

  10.   

    XmlStr = "<?xml version=""1.0"" encoding=""gb2312""?><root></root>"
      

  11.   

    xml操作类,牛人改一个功能强大一点的.
    贴上来吧,我上新手大家不在见笑哈Option Explicit
    Private xml_document As DOMDocument
    Private values_node As IXMLDOMNode
    Private p_AppPath, FileName, TopTag, XmlStr As String
    Private IsOpen As Boolean'****************************
    'Init 模块初始化  ok
    '****************************
    Public Function Init()
        p_AppPath = App.Path
        FileName = "\Text.xml"
        TopTag = "UserList"
        Set xml_document = New DOMDocument
    End Function'*****************************
    'LoadFiles 载入现有文件 ok
    '*****************************
    Public Function LoadFiles()  xml_document.Load p_AppPath & FileName
        If xml_document.documentElement Is Nothing Then
           Exit Function
        End If
      Set values_node = xml_document.documentElement
      IsOpen = TrueEnd Function'****************************
    'NewXmlDoc  建立新的xml文档
    '同时设置顶层节点TopTag
    '****************************
    Public Function NewXmlDoc()
        XmlStr = "<?xml version=""1.0"" encoding=""gb2312""?><" & TopTag & "></" & TopTag & ">"
        xml_document.loadXML XmlStr
        Set values_node = xml_document.documentElement
          ' 建立XML文件
         If values_node Is Nothing Then
           Exit Function
         End If'Set values_node = xml_document.createElement(TopTag)
    'xml_document.appendChild values_node
    IsOpen = TrueEnd Function'*************************************
    'GetNodeValue 返回各个节点的值
    'start_at_node 父节点名称
    'node_name 子节点名称
    'default_value 默认节点值
    '*************************************
    Public Function GetNodeValue(ByVal start_at_node As String, ByVal node_name As String, ByVal default_value As String) As String
      Dim search_node As IXMLDOMNode
      Dim Obj_Node As IXMLDOMNode
      If IsOpen = False Then Exit Function
      Set search_node = values_node.selectSingleNode(start_at_node)
      Set Obj_Node = search_node.selectSingleNode(node_name)
      If Obj_Node Is Nothing Then
        GetNodeValue = default_value
      Else
        GetNodeValue = Obj_Node.Text
      End IfEnd Function'***************************
    ' SaveFiles  保存xml文档 ok
    '***************************
    Public Function SaveFiles()
        xml_document.save p_AppPath & FileName
    End Function'***************************************************
    'CreateNode新建节点 ok
    'parent  父节点名称
    'node_name  子节点名称
    'node_value 为空建立子节点,并把节点对象设置为新建的节点.
    '***************************************************
    Public Sub CreateNode(ByVal Parent As String, ByVal node_name As String, ByVal node_value As String)
      Dim new_node As IXMLDOMNode
      Dim Parent_Node As IXMLDOMNode
      
     If IsOpen = False Then Exit Sub
     Set Parent_Node = values_node.selectSingleNode(Parent)  If Trim(node_value) <> "" Then
          Set new_node = Parent_Node.ownerDocument.createElement(node_name)
          new_node.Text = node_value
          Parent_Node.appendChild new_node
          
      Else
          Set new_node = Parent_Node.ownerDocument.createElement(node_name)
          new_node.Text = node_value
          Parent_Node.appendChild new_node
          Set values_node = values_node.selectSingleNode(node_name)
      End If
      
    End Sub'********************************
    'DeleteNode 删除节点
    '********************************
    Public Sub DeleteNode(ByVal Parent As String, ByVal Obj_Node As String)Dim Dele_Node As IXMLDOMElement
    Dim Parent_Node As IXMLDOMElementIf IsOpen = False Then Exit Sub
    Set Parent_Node = values_node.selectSingleNode(Parent)  Set Dele_Node = Parent_Node.selectSingleNode(Obj_Node)
      If Dele_Node Is Nothing Then
         Exit Sub
      Else
        Parent_Node.removeChild Dele_Node
    End If
      
    End Sub'********************************
    'SetAttr 设置节点属性
    'Obj_Node 节点名称
    'AttrName 节点属性名称
    'AttrValue 节点属性值
    '*******************************
    Public Sub SetAttr(ByVal Obj_Node As String, ByVal AttrName As String, ByVal AttrValue As String)
    Dim Attr_Node As IXMLDOMElement
    Dim Parent_Node As IXMLDOMElementIf IsOpen = False Then Exit Sub
    Set Parent_Node = values_node.selectSingleNode(Obj_Node)
        Set Attr_Node = Parent_Node
        Attr_Node.setAttribute AttrName, AttrValue
    End Sub'*****************************
    'EditNodeValue 修改节点值
    'EditName 节点名称
    'NewValue 节点新值
    '*****************************
    Public Sub EditNodeValue(ByVal EditName As String, ByVal NewValue As String)
    Dim Edit_Node As IXMLDOMElement
    Set Edit_Node = values_node.selectSingleNode(EditName)
    If Edit_Node Is Nothing Then
      MsgBox "not found"
      Exit Sub
    Else
      Edit_Node.Text = NewValue
    End IfEnd Sub