AppendChild xml C ++ doesn't work as needed

Why, after execution appendChild

, I cannot read the next node?

(newChildList->get_item(count,&pNewChild))

      

The API appendChild

doesn't work the way it should. After I put this line in, I cannot get the next one get_item()

in the loop. It returns pNewChild

which is NULL

. Why is this happening?

IXMLDOMNode *pNewChild=NULL,*outNewChild;
IXMLDOMNodeList *newChildList=NULL;
BSTR newName;
long len=0;
HRESULT hr=S_OK;

//aggregate attribute
//CHK_HR(aggregateAttribute(pNodeNew,pXMLDomSrc,pXMLDomNew));

//read the list of child
CHK_HR(pNodeNew->get_childNodes(&newChildList));
CHK_HR(newChildList->get_length(&len));
//go over all the children
for(long count=0;count<len;count++)
{       
    hr =newChildList->get_item(count,&pNewChild);
    CHK_HR(pNewChild->get_nodeName(&newName));

    USES_CONVERSION;
    string temp= W2T(newName);
    temp = temp.substr(temp.find_first_of("/:")+1);
    if (!temp.compare("tuple")) 
    {       
        //CHK_HR(aggregateTuple(pXMLDomSrc,pNewChild));
    }       
    else 
    {       
        CHK_HR(pXMLElement->appendChild(pNewChild,&outNewChild));
    }
    SAFE_RELEASE(pNewChild);
    //SysFreeString(newName);
}

CleanUp:
//SAFE_RELEASE(newChildList);
SAFE_RELEASE(pNewChild);
return hr;

      

+3


source to share


1 answer


appendChild moves node to a different location, shrinking the list of children each time.

Try the following:



// reverse loop from index base 0 "len-1" to 0
for(long count=len-1;count>=0;--count)

      

On a side note, you seep a lot of memory ( outNewChild

and newName

)

+2


source







All Articles