您的位置:首页 > 编程语言 > VB

excel vba xml文件操作

2016-12-07 18:26 405 查看
<mainnote>

     <subnote>

    </subnote>

    <subnote>

    <subnote>

</mainnote>

Dim wsXml As Worksheet

Dim xmlDoc As Object

Dim objDOM, n, nodes, subNodes As Object, ns, i, j

Set objDOM = CreateObject("MSXML.DOMDocument")

objDOM.Load (ThisWorkbook.Path & "\xxxxxx.xml")

objDOM.async = False

strPath = Left(strPath, Len(strPath) - Len(strCbf) - 12)

Set ns =objDOM.SelectNodes("//mainnote//subnote") 
  'ns就得到了所有subnote的一个集合

 Debug.Print ns.Length

Dim k

For i = 1 Tons.Length        '可以用Length遍历所有subnote

       If ns.Item(i - 1).ChildNodes(0).Text <> "xxx" Then 'name    '用Item() 去取单独subnote

           

          '用SelectSingleNode 根据字符串筛选要得到的某个node

          '用GetEleentsByTagname 得到node下的复合tagname条件的所有标签

        Set subNodes = ns.Item(i-1).SelectSingleNode("id").getElementsByTagName_r("name")

           Debug.Print subNodes.Length

           For k = 0 To subNodes.Length - 1

               

               Debug.Print subNodes.Item(k).Text

               

               If (subNodes.Item(k).Text = "ID") Then

                   wsXml.Cells(index, "E") = "Yes"

               End If

                   

            

               

           Next
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: