c++ - appendChild xml c++ 无法按需工作

标签 c++ xml msdn

为什么在执行appendChild后,我无法读取下一个节点?

(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;

最佳答案

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

试试这个:

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

附带说明一下,您泄漏了大量内存(outNewChildnewName)

关于c++ - appendChild xml c++ 无法按需工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14919199/

相关文章:

c++ - 将智能指针放入 STL 容器中

c++ - CMake:无法运行 MSBuild 命令:MSBuild.exe

c++ - C++ 中初始化的结果是什么?

Android 设计布局 ListItem RecyclerView Horizo​​ntal

javascript - 我如何使用 jquery ajax() 和 $.when() 方法从 .xml 文档获取 xml 节点文本/值

c++ - CreateFile() 返回地址为 FFFFFF 的无效指针

c++ - Winapi:编辑控件未扩展其缓冲区

c# - File.AppendAllText导致程序第二次运行时抛出访问异常

c++ - 是否可以跨进程使用函数指针?

java - Android TextView 应该自动垂直滚动