继续例子程序,IXMLDOMDocument实现保存一个简单的XML文件
如题,谢谢各位
指教.
[解决办法]
/************************************************************************/
/*Load the XML profiles of main menu, when start */
/************************************************************************/
LRESULT XMainMenuMainFrame::LoadXMLProfiles()
{
LRESULT hr = S_OK;
VARIANT_BOOL vbResult;
IXMLDOMDocument *pXMLDOMDocument = NULL;
IXMLDOMNode *pNodeGlobal = NULL;
IXMLDOMNode *pNodePage = NULL;
IXMLDOMNode *pNodeItem = NULL;
IXMLDOMNode *pNodeDock = NULL;
IXMLDOMNodeList *pNodePageList = NULL;
IXMLDOMNodeList *pNodeItemList = NULL;
long lPageCount = 0;
long lListItemCount = 0;
CString strText;
VARIANT vSaveFilePath;
VARIANT vSaveBackupFilePath;
::CoInitializeEx( NULL, COINIT_MULTITHREADED );
VariantInit(&vSaveFilePath);
V_VT(&vSaveFilePath) = VT_BSTR;
vSaveFilePath.bstrVal = SysAllocString(MENU_XML);
VariantInit(&vSaveBackupFilePath);
V_VT(&vSaveBackupFilePath) = VT_BSTR;
vSaveBackupFilePath.bstrVal = SysAllocString(MENU_BACKUP_XML);
hr = CoCreateInstance(CLSID_DOMDocument, NULL, CLSCTX_INPROC_SERVER
[解决办法]
CLSCTX_LOCAL_SERVER, IID_IXMLDOMDocument, (LPVOID*)&pXMLDOMDocument);
if (FAILED(hr))
{
goto Error;
}
hr = pXMLDOMDocument->load(vSaveFilePath, &vbResult);
if (FAILED(hr)
[解决办法]
!vbResult)
{
hr = pXMLDOMDocument->load(vSaveBackupFilePath, &vbResult);
if (FAILED(hr)
[解决办法]
!vbResult)
{
goto Error;
}
}
hr = pXMLDOMDocument->selectSingleNode(ROOT_KEY, &pNodeGlobal);
if (FAILED(hr)
[解决办法]
!pNodeGlobal)
{
goto Error;
}
//load page content
hr = pNodeGlobal->selectNodes(PAGE_KEY, &pNodePageList);
if (FAILED(hr)
[解决办法]
!pNodePageList)
{
goto Error;
}
hr = pNodePageList->get_length(&lPageCount);
if (FAILED(hr)
[解决办法]
lPageCount <= 0)
{
goto Error;
}
for (long lPageIndex = 0; lPageIndex < lPageCount; lPageIndex++)
{
hr = pNodePageList->get_item(lPageIndex, &pNodePage);
if (FAILED(hr)
[解决办法]
!pNodePage)
{
RELEASE_OBJ(pNodePage);
continue;
}
strText = GetNodeText(pNodePage, PAGE_ID);
int i = _wtoi(strText);
strText.ReleaseBuffer();
hr = pNodePage->selectNodes(PAGE_LISTITEM_KEY, &pNodeItemList);
if (FAILED(hr)
[解决办法]
!pNodeItemList)
{
RELEASE_OBJ(pNodePage);
RELEASE_OBJ(pNodeItemList);
continue;
}
hr = pNodeItemList->get_length(&lListItemCount);
if (FAILED(hr)
[解决办法]
lListItemCount <= 0)
{
RELEASE_OBJ(pNodePage);
RELEASE_OBJ(pNodeItemList);
continue;
}
for (long lListItemIndex = 0; lListItemIndex < lListItemCount; lListItemIndex++)
{
hr = pNodeItemList->get_item(lListItemIndex, &pNodeItem);
if (FAILED(hr)
[解决办法]
!pNodeItem)
{
RELEASE_OBJ(pNodeItem);
continue;
}
MainMenuItem plusData;
plusData._EmptyItem = FALSE;
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_IMAGE);
plusData._strImage = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_TEXT);
plusData._strText = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_FILE);
plusData._strFile = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_PARAM);
plusData._strParam = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_SHELL);
if (1 == _wtoi(strText))
{
plusData._bIsExeShell = TRUE;
}
else
{
plusData._bIsExeShell = FALSE;
}
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem,PAGE_LISTITEM_EXE_TYPE);
//plusData._iStartNotShell = _wtoi(strText);
strText.ReleaseBuffer();
//_mainList->AppendItem(plusData._strImage, plusData._strText, plusData);
RELEASE_OBJ(pNodeItem);
}
RELEASE_OBJ(pNodePage);
RELEASE_OBJ(pNodeItemList);
}
//load dock content
hr = pNodeGlobal->selectSingleNode(DOCK_KEY, &pNodeDock);
if (FAILED(hr)
[解决办法]
!pNodeDock)
{
goto Error;
}
{
hr = pNodeDock->selectNodes(PAGE_LISTITEM_KEY, &pNodeItemList);
if (FAILED(hr)
[解决办法]
!pNodeItemList)
{
RELEASE_OBJ(pNodeDock);
RELEASE_OBJ(pNodeItemList);
goto Error;
}
hr = pNodeItemList->get_length(&lListItemCount);
if (FAILED(hr)
[解决办法]
lListItemCount <= 0)
{
RELEASE_OBJ(pNodeDock);
RELEASE_OBJ(pNodeItemList);
goto Error;
}
for (long lListItemIndex = 0; lListItemIndex < lListItemCount; lListItemIndex++)
{
hr = pNodeItemList->get_item(lListItemIndex, &pNodeItem);
if (FAILED(hr)
[解决办法]
!pNodeItem)
{
RELEASE_OBJ(pNodeItem);
continue;
}
MainMenuItem plusData;
plusData._EmptyItem = FALSE;
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_IMAGE);
plusData._strImage = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_TEXT);
plusData._strText = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_FILE);
plusData._strFile = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_PARAM);
plusData._strParam = strText;
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem, PAGE_LISTITEM_SHELL);
if (1 == _wtoi(strText))
{
plusData._bIsExeShell = TRUE;
}
else
{
plusData._bIsExeShell = FALSE;
}
strText.ReleaseBuffer();
strText = GetNodeText(pNodeItem,PAGE_LISTITEM_EXE_TYPE);
//plusData._iStartNotShell = _wtoi(strText);
strText.ReleaseBuffer();
//_mainList->AppendItem(plusData._strImage, plusData._strText, plusData);
RELEASE_OBJ(pNodeItem);
}
RELEASE_OBJ(pNodeItemList);
}
RELEASE_OBJ(pNodeDock);
RELEASE_OBJ(pNodePageList);
RELEASE_OBJ(pNodeGlobal);
RELEASE_OBJ(pXMLDOMDocument);
VariantClear(&vSaveFilePath);
VariantClear(&vSaveBackupFilePath);
::CoUninitialize();
return S_OK;
Error:
RELEASE_OBJ(pXMLDOMDocument);
RELEASE_OBJ(pNodeGlobal);
RELEASE_OBJ(pNodePage);
RELEASE_OBJ(pNodeItem);
RELEASE_OBJ(pNodePageList);
RELEASE_OBJ(pNodeItemList);
VariantClear(&vSaveFilePath);
VariantClear(&vSaveBackupFilePath);
::CoUninitialize();
return hr;
}