VBA/Excel/Access/Word/XML/DOMDocument

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

Find and Delete all Nodes for the Employee Bullen

 
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



Find and Edit the Node that Contains the FirstName Mike

 
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



Find by path

 
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



Find by string value

 
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



Find by value

 
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