Click to See Complete Forum and Search --> : insertBefore


jimeh
August 30th, 2001, 05:18 AM
Sorry if anyone feels I'm posting in the wrong forum, but this question did not really fit in in the VC++ forum either.

My problem is that I can't get the insertBefore method to work when adding new elements/nodes to my xml document.

This is my xml document:
&lt?xml version="1.0" encoding="ISO-8859-1"?&gt
&ltnames&gt
&ltname name="Flanders, Ned"/&gt
&ltname name="Gumble, Barney"/&gt
&ltname name="Skinner, Seymour"/&gt
&lt/names&gt

Now I would like to do a insert with a new name "Simpson, Homer" so that it is placed before "Skinner, Seymour".

According to the MSDN specification the insertBefore method takes two arguments: newChild and refChild. But what confuses me is that refChild is supposed to be a variant, and not just a node pointer. I've tried different approaches but I've only managed to insert the new name at the end of the list.

Does anyone have some code samples using insertBefore in VC++? I would be very grateful. I've been looking for examples everywhere but found none that helps me.

Thanks!

JC77
December 7th, 2001, 06:21 PM
MSXML2::IXMLDOMNodePtr pnode = pnodeList->item[pos];
_variant_t refchild = _variant_t(pnode, false);
parent->insertBefore(pnode, refchild);

I got the above code compiled successfully and I know the node was inserted correctly; however, if I retreved the node value of refchild, it would GPF. Does anyone know the problem? Thanks.

da5id
April 20th, 2002, 01:55 PM
what about that:
MSXML::IXMLDOMNodePtr spifcNodeFirstChild = NULL;
MSXML::IXMLDOMNodePtr spifcNode = NULL;
MSXML::IXMLDOMElementPtr spifcElement = NULL;
...
spifcNodeFirstChild = spifcFzNode->firstChild;
_variant_t varRefChild;
varRefChild.vt = VT_DISPATCH;
varRefChild.ppdispVal = NULL;
if (spifcNodeFirstChild != NULL)
varRefChild = (IDispatch*)spifcNodeFirstChild;
...
spifcNode->insertBefore(spifcElement, varRefChild);