none
Listing CustomXMLPart Node Data RRS feed

  • Question

  • I am working on a project where I need to parse a customXMLPart and list information about each node. 

    I've worked out a basic process, but unfortunately it only looks two levels deep and to avoid creating a hairball, I thought I would share what I have so far and see if anyone had any ideas for a recursive function so my list would be complete regardless of the levels involved.  In the following code, I've added a customXMLPart where one node contains multiple child nodes.  It works, but how would I modify this code so that it would still return a a complete list if oee of the child nodes also had multiple child nodes.  Thanks!!

    Sub Testing()
    Dim oCustXMLPart As CustomXMLPart, oNode As CustomXMLNode, oNodeChild As CustomXMLNode, oNodeChildChild As CustomXMLNode
    Dim i As Long
      On Error Resume Next
      ActiveDocument.CustomXMLParts(4).Delete
      On Error GoTo 0
      Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add
      oCustXMLPart.LoadXML ("<DocElement><Name>Joe</Name><Age>41</Age><Multi><Item1>A</Item1><Item2>B</Item2></Multi></DocElement>")
      Set oNode = oCustXMLPart.DocumentElement
      For Each oNodeChild In oNode.ChildNodes
        If oNodeChild.NodeType = msoCustomXMLNodeElement Then
          Debug.Print oNodeChild.BaseName
          Select Case oNodeChild.ChildNodes.Count
              Case Is > 0
                If oNodeChild.ChildNodes(1).NodeType = msoCustomXMLNodeElement Then
                  Debug.Print oNodeChild.Text & " Multi Node"
                  For Each oNodeChildChild In oNodeChild.ChildNodes
                    Debug.Print oNodeChildChild.BaseName
                    If oNodeChildChild.NodeType = msoCustomXMLNodeElement Then
                      Debug.Print oNodeChildChild.Text & " Single Node"
                    End If
                  Next oNodeChildChild
                Else
                  Debug.Print oNodeChild.Text & " Single Node"
                End If
            Case Else
              Debug.Print oNodeChild.Text & " Single Node"
              i = i + 1
          End Select
         End If
      Next oNodeChild
    lbl_Exit:
      Exit Sub
    End Sub

     


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Monday, July 30, 2012 10:38 PM

Answers

  • Tom,

    Thanks. It is late and I will be out most of the day tomorrow.  I think I can make this work, but I will have to study it a bit and perhaps adjust to fit my particular needs.

    P.S.  I have never used a TreeView control before.  When I went to add it to the toolbox I first added the version 6.0 and got an error that the library wasn't registered.  I added the 5.0 version with no problem.  Any idea why I got that error or how to fix.  Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by Greg Maxey Wednesday, August 1, 2012 6:25 AM
    Tuesday, July 31, 2012 3:03 AM

All replies

  • Hi Greg,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that you need a recursive function to iterator every elements of your xmldoc. I will show a sample for you to address that. This is a vba form, in the form I use a treeview control to show xml document's struct. Yon can modify it to fit your request.

    Private Sub UserForm_Activate()
        Dim oCustXMLPart As CustomXMLPart
        Dim oNode As CustomXMLNode
        Dim oNodeChild As CustomXMLNode
        Dim oNodeChildChild As CustomXMLNode
        Dim i As Long
        Dim tRoot As Node
        
        On Error Resume Next
        ActiveDocument.CustomXMLParts(4).Delete
        On Error GoTo 0
        Set oCustXMLPart = ActiveDocument.CustomXMLParts.Add
        oCustXMLPart.LoadXML ("<DocElement><Name>Joe</Name><Age>41</Age><Multi><Item1>A</Item1><Item2>B</Item2></Multi></DocElement>")
        Set oNode = oCustXMLPart.DocumentElement
        Set tRoot = TreeView1.Nodes.Add
        tRoot.Text = "DocElement"
        tRoot.Key = "DocElement"
        Iterator oNode, tRoot
    End Sub
    
    Sub Iterator(ByRef Element As CustomXMLNode, ByRef tNode As Node)
        Dim Childe As CustomXMLNode
        Dim subNode As Node
        For Each Childe In Element.ChildNodes
            If Childe.NodeType = msoCustomXMLNodeText Then
                Set subNode = TreeView1.Nodes.Add(tNode.Key, tvwChild, Childe.XPath, Childe.Text)
            ElseIf Childe.NodeType = msoCustomXMLNodeElement Then
                Set subNode = TreeView1.Nodes.Add(tNode.Key, tvwChild, Childe.XPath, Childe.BaseName)
                If Childe.HasChildNodes Then
                    Iterator Childe, subNode
                End If
            End If
        Next
    End Sub
    

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 31, 2012 2:33 AM
    Moderator
  • Tom,

    Thanks. It is late and I will be out most of the day tomorrow.  I think I can make this work, but I will have to study it a bit and perhaps adjust to fit my particular needs.

    P.S.  I have never used a TreeView control before.  When I went to add it to the toolbox I first added the version 6.0 and got an error that the library wasn't registered.  I added the 5.0 version with no problem.  Any idea why I got that error or how to fix.  Thanks.


    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    • Marked as answer by Greg Maxey Wednesday, August 1, 2012 6:25 AM
    Tuesday, July 31, 2012 3:03 AM
  • Hi Greg,

    How to use treeview isn't the goal for our aim. I just use snippet to explain how to iterator the xml elements. I think the most important thing is use iterator.

    P.S. I have never used a TreeView control before. When I went to add it to the toolbox I first added the version 6.0 and got an error that the library wasn't registered. I added the 5.0 version with no problem. Any idea why I got that error or how to fix.

    It's hard to say why. Perhaps the ocx file missed, maybe the registry has been changed. It need more detailed information to do troubleshooting.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 31, 2012 7:24 AM
    Moderator