Link to home
Start Free TrialLog in
Avatar of NikWhitfield
NikWhitfield

asked on

Node mapping between DOMDocuments in MSXML4

Hi,

   I have a particular IXMLDOMNode object on a DOMDocument40. I have a second DOMDocument40, which is exactly the same as the first, with the exception that its nodes have a vital value stored in the .key property. Question is, how do I get hold of the same node in the second DOMDoc, thereby gaining access to its .key property??

Cheers,

Nik
Avatar of BigRat
BigRat
Flag of France image

I presume you mean to do this generically. The problem is however that the XPath to the node in tree 1 is not obtainable by a "backward translation" API DOM call from the "currently selected node". This literally implies that when navigating in tree 1 you have to apply exactly the same navigation to tree 2.

The other alternative might be to try to construct a navigation by recursively selecting the parent node up to the root, ascertain in which position this is in the child list, and in such a way construct an XPath expression to use in the second DOM. You don't necessarily have to construct an XPath expression, since you can apply the selection to the second tree after ascertaining the "selection at this level" viz:

   function selectInTree2(tree1,tree2) {
       if IsRootNode(tree2) {
          /* at root so return root */
          return tree2.selectNode('/');
       } else {
          /* first get parent */
          parent = selectParent(tree1);
          /* and then determine which child */
          position = findChild(parent,tree1);
          /* now tree2 is equivalent to parent */
          /* so just select the nth child */
          return tree2.selectNodeByPosition(position)
       }
   }

if you get my drift(?)!

HTH
Avatar of NikWhitfield
NikWhitfield

ASKER

This doesn't make much sense to me.....

I can see how you'd do it with XPath, but not by using the code fragment above. Could you explain it further? I can't see how findChild would work, hence how the whole thing would work!

Much appreciated,

Nik
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, I got ya. Works a treat - good use of recursion. Thanks a lot
You managed in an hour to get all that to work! I am impressed! Thanks for the cheese.
Here's the code, if you ever need it (and happen to be delving into VB....)

'**********************************************************************
Function getNode(pobjXMLNode As IXMLDOMNode, pobjDomDoc As DOMDocument40) As IXMLDOMNode
'**********************************************************************
'* Name:        getNode
'*
'* Description: Finds a node from one tree in another identical tree
'*
'* Parameters:  pobjXMLNode - the node to search for. pobjDomDoc - the tree to search in
'* Returns:     IXMLDomNode - the equivalent node in the other tree
'*
'*
'* Notes :      A 'hard to get your head round' piece of recursion here. Continue to
'*              pass an object's parent through recursively, until it's the root node
'*              being passed, which is the terminating condition. At that point,
'*              return the Root of the searching tree, which will then be used to
'*              return the correct child recursively. Easy!
'*
'* Created:     11/07/2002: NMW: Creation
'*
'**********************************************************************

Const PROC_NAME = "getNode"
On Error GoTo ErrorHandler

'Body Code Begins

Dim objXMLParent As IXMLDOMNode
Dim lngPosition As Long
Dim objParentInTree As IXMLDOMNode

    'RECURSIVE TERMINATING CONDITION
    'Check whether this node is the tree root - if so, return it.
    If pobjXMLNode.nodeName = mcstrDOCUMENT_ROOT_NAME Then
        Set getNode = pobjDomDoc.selectSingleNode("/")
    'Not the root node, so it must have a parent - select it
    Else
        Set objXMLParent = pobjXMLNode.ParentNode
        lngPosition = Position(pobjXMLNode)
        'RECURSIVE CALL MADE HERE
        Set objParentInTree = getNode(objXMLParent, pobjDomDoc)
        Set getNode = objParentInTree.ChildNodes(lngPosition)
    End If

'Body Code Ends

LeaveProc:

Exit Function
ErrorHandler:
   Select Case Err.Number
       Case Else
            ' Then raise it
            Err.Raise gobjErr.Number, gobjErr.Source, gobjErr.Description, gobjErr.HelpFile, gobjErr.HelpContext
   End Select
End Function
'**********************************************************************

Ta