VBA/Excel/Access/Word/XML/DOMDocument

Материал из VB Эксперт
Перейти к: навигация, поиск

Find and Delete all Nodes for the Employee Bullen

   <source lang="vb">

Sub DeleteNode()

          Dim oXmlDoc As DOMDocument
          Dim oXmlNode As IXMLDOMNode
          Dim oXmlNodes As IXMLDOMNodeList
          Set oXmlDoc = New DOMDocument
          oXmlDoc.async = False
          oXmlDoc.Load (ThisWorkbook.Path & "\EmployeeSalesTest.xml")
          Set oXmlNodes = oXmlDoc.SelectNodes("//Employee[LastName="Bullen"]")
          For Each oXmlNode In oXmlNodes
          oXmlNode.parentnode.RemoveChild oXmlNode
          Next
          oXmlDoc.Save ThisWorkbook.Path & "\EmployeeSalesTest.xml"

End Sub

</source>
   
  


Find and Edit the Node that Contains the FirstName Mike

   <source lang="vb">

Sub ChangeNode()

        Dim oXmlDoc As DOMDocument
        Dim oXmlNode As IXMLDOMNode
        FileCopy ThisWorkbook.Path & "\EmployeeSales.xml", _
        ThisWorkbook.Path & "\EmployeeSalesTest.xml"
        Set oXmlDoc = New DOMDocument
        oXmlDoc.async = False
        oXmlDoc.Load (ThisWorkbook.Path & "\EmployeeSalesTest.xml")
        Set oXmlNode = oXmlDoc..SelectsingleNode ("//FirstName[text()="Mike"]")
        oXmlNode.Text = "Michael"
        oXmlDoc.Save ThisWorkbook.Path & "\EmployeeSalesTest.xml"

End Sub

</source>
   
  


Find by path

   <source lang="vb">

Sub FindNode()

        Dim oXmlDoc As DOMDocument
        Dim oXmlNode As IXMLDOMNode
        Dim oXmlNodes As IXMLDOMNodeList
          Set oXmlDoc = New DOMDocument
          oXmlDoc.async = False
          oXmlDoc.Load (ThisWorkbook.Path & "\EmployeeSales.xml")
          Set oXmlNodes = oXmlDoc.SelectNodes("/EmployeeSales/Employee/Empid")
          For Each oXmlNode In oXmlNodes
          Debug.Print oXmlNode.Text
          Next

End Sub

</source>
   
  


Find by string value

   <source lang="vb">

Sub FindNode()

          Dim oXmlDoc As DOMDocument
          Dim oXmlNode As IXMLDOMNode
          Set oXmlDoc = New DOMDocument
          oXmlDoc.async = False
          oXmlDoc.Load (ThisWorkbook.Path & "\EmployeeSales.xml")
          Set oXmlNode = oXmlDoc.SelectsingleNode ("//FirstName[text()="Mike"]")
          Debug.Print oXmlNode.XML

End Sub

</source>
   
  


Find by value

   <source lang="vb">

Sub FindNode()

          Dim oXmlDoc As DOMDocument
          Dim oXmlNode As IXMLDOMNode
          Dim oXmlNodes As IXMLDOMNodeList
          Set oXmlDoc = New DOMDocument
          oXmlDoc.async = False
          oXmlDoc.Load (ThisWorkbook.Path & "\EmployeeSales.xml")
          Set oXmlNodes = oXmlDoc.SelectNodes("//Employee[InvoiceAmount>3000]")
          For Each oXmlNode In oXmlNodes
          Debug.Print oXmlNode.Text
          Next

End Sub

</source>