Issue with XML XPath

speakers_86

Registered User.
Local time
Yesterday, 23:58
Joined
May 17, 2007
Messages
1,919
Heres my code


Code:
Sub test()
    Dim objdom As DOMDocument
    Dim objRootElem As IXMLDOMElement

    Set objdom = New DOMDocument

    ' Creates root element
    Set objRootElem = objdom.CreateElement("Books")
    objdom.appendChild objRootElem
    
    Dim node As IXMLDOMElement
Debug.Print "a"
    Set node = CreateNode("Books", "aNewNode", objdom)
Debug.Print "b"
    Set node = CreateNode("//Books/aNewNode/", "Test", objdom)
Debug.Print "c"
    CreateAttrib node, "StarWars", "ANewHope", objdom
    
    Dim test As IXMLDOMElement
    Set test = objdom.selectSingleNode("//aNewNode[@StarWars='ANewHope']")      
    CreateAttrib test, "ANewAttribute", "SomeValue", objdom                              'breaks it
Debug.Print "d"
    CreateAttrib node, "id", "GreatMovies", objdom
Debug.Print "e"
    'CreateNode "GreatMovies", "ChildTest", objdom, True
    ' Saves XML data to disk.
    objdom.Save ("C:\Users\Kanyer\Desktop\andrew.xml")
    Set objdom = Nothing
    Set objRootElem = Nothing
End Sub

Code:
Public Sub CreateAttrib(node As IXMLDOMElement, _
                        strAttrib As String, _
                        strValue As String, _
                        objdom As DOMDocument)
    'Create an attribute in the specified dom and node
    'Parameters
    '    strNode    enter the name of the node to modify
    '    strAttrib  enter the name of your attribute
    '    strValue   enter the value the attribute will assume
    '    objdom     reference to the document
    'example  <strNode strAttrib=strValue/>
    'Adapted from http://www.freevbcode.com/ShowCode.asp?ID=1919
    'by speakers_86
    
    Dim attrib As IXMLDOMAttribute
    Set attrib = objdom.createAttribute(strAttrib)
    attrib.nodeValue = strValue

    node.setAttributeNode attrib                                     'This line is highlighted

    'Set node = Nothing
    Set attrib = Nothing
End Sub

Code:
Public Function CreateNode(strParent As String, _
                           strNode As String, _
                           objdom As DOMDocument, _
                           Optional ByID As Boolean) As IXMLDOMElement
    'Add a node to the specified dom
    'Parameters
    '   strParent   node will be created as a child of this node
    '   strNode     basename of your node; ex. <strNode\>
    '   objdom      reference to the document
    'example    <strParent><strNode></strParent>
    'Adapted from http://www.freevbcode.com/ShowCode.asp?ID=1919
    'by speakers_86
     
    Dim node   As IXMLDOMNode
    Dim parent As IXMLDOMElement
    Dim nodeList As IXMLDOMNodeList
    If ByID = True Then
        Set nodeList = objdom.selectNodes(strParent)
        Set parent = nodeList(1)
    Else
       Set parent = objdom.selectSingleNode(strParent)
    End If
    Set node = objdom.CreateElement(strNode)
    Set CreateNode = parent.appendChild(node)
    

    Set parent = Nothing
    Set node = Nothing
    On Error Resume Next
    Set nodeList = Nothing
End Function



The error at line:
Code:
node.setAttributeNode attrib
Is Err 91, With block or object variable not set. That tells me that node is node is not getting set correctly by the XPath. Any ideas?
 
Last edited:
I remember trying to do some XPath stuff and only very basic queries ran, but complex stuff didn't. And I believe some versions of the DOMDocument don't fully support XPath. I don't remember which versions but you will need to search for the right version and use that and hopefully that should fix it.
 
I am EXTATIC to get feedback on this. I thought I was on my own. And I think you may be on to something. When declaring the domDocument, I just ignored the other options since I didn't know what they are. But now I see that DomDocument60 is literally version 6. Maybe that is the problem. I'll report back tomorrow when I mess with it. I'm not in the state of mind to do it now. ;)

I believe this is the last big piece of the puzzle for my Ribbon Builder v0.2. I redid it from the ground up, and I think it is way better now. It's similar to the other tool out there, I think it is called EZRibbon Builder, except that mine stores all nodes and attributes in a table for easier editing. I'll be quite happy if I can get this worked out!
 
If all else fails you might just have to revert to recursive traversal of the document. It's a ribbon builder so the XML should be fairly small and quick to traverse.

Let us know how things pan out.
 
Thanks, I got it sorted. I'm using DomDocument60, seems to be working now.
 
Good to know. Yes the documentation is very unclear about these things. I think they should make a comparison chart of each version showing their differences. It took me hours of digging to find out what the problem was at the time.
 

Users who are viewing this thread

Back
Top Bottom