2013-02-17 55 views
3

为什么在做appendChild之后,我无法读取下一个节点?appendChild xml C++不按需要工作

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

appendChild API不工作,因为它应该是。我把这一行后,我无法得到循环中的下一个get_item()。它返回pNewChild这是NULL。为什么会发生?

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; 

回答

2

appendChild将节点移动到别处,每次将子列表减一。

试试这个:

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

在一个侧面说明,您泄漏大量内存(outNewChildnewName