MSXML2::IXMLDOMNodePtr CXmlBase::CreateRootNode(MSXML2::IXMLDOMDocument2Ptr pDoc , LPCTSTR lpszRootText) { HRESULT hr = S_OK; MSXML2::IXMLDOMElementPtr pElement = NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; CComBSTR bstrRootText = _bstr_t("root").GetBSTR(); if (pDoc == NULL) return NULL; if (lpszRootText != NULL) bstrRootText = lpszRootText; if (bstrRootText.Length() == 0) return NULL; try { //hr = pDoc->raw_createElement(bstrRootText.m_str, &pElement); //if (FAILED(hr) || pElement == NULL) return NULL; //pNode = pElement; pNode = pDoc->createNode(NODE_ELEMENT, bstrRootText.m_str, _T("")); if (pNode == NULL) return NULL; //hr = pDoc->raw_appendChild(pNode, NULL); //if (FAILED(hr)) return NULL; pNode = pDoc->appendChild(pNode); } catch(_com_error &e) { CString str; str.Format(_T("创建xml根节点发生异常。\r\nComError:\r\n%s") , e.ErrorMessage()); ::MessageBox( NULL, str, g_sAppTitle, MB_OK|MB_ICONERROR); return NULL; } catch(...) { return NULL; } return pNode; }
MSXML2::IXMLDOMNodePtr CXmlBase::InsertNode( MSXML2::IXMLDOMNodePtr pParentNode , MSXML2::IXMLDOMNodePtr pNodeBack , LPCTSTR lpszNodeText ) { HRESULT hr = S_OK; MSXML2::IXMLDOMDocument2Ptr pDoc = NULL; MSXML2::IXMLDOMElementPtr pElement = NULL; MSXML2::IXMLDOMNodePtr pNode = NULL; CComBSTR bstrNodeText = _bstr_t("node").GetBSTR(); if (pParentNode == NULL || lpszNodeText == NULL) return NULL; bstrNodeText = lpszNodeText; if (bstrNodeText.Length() == 0) return NULL; try { pDoc = pParentNode->GetownerDocument(); if (pDoc == NULL) return NULL; //hr = pDoc->raw_createElement(bstrNodeText.m_str, &pElement); //if (FAILED(hr) || pElement == NULL) return NULL; pNode = pDoc->createNode(NODE_ELEMENT, bstrNodeText.m_str, _T("")); if (pNode == NULL) return NULL; CComVariant varNode = pNodeBack.GetInterfacePtr(); pNode = pParentNode->insertBefore(pNode, varNode); } catch(_com_error &e) { CString str; str.Format(_T("插入xml节点发生异常。\r\nComError:\r\n%s") , e.ErrorMessage()); ::MessageBox(NULL,str, g_sAppTitle, MB_OK|MB_ICONERROR); return NULL; } catch(...) { return NULL; } return pNode; }
bool Engine::LoadContent() { // Setup our SpriteBatch. m_pSpriteBatch = new DirectX::SpriteBatch(d3dContext_); m_pFont->Initialize(d3dDevice_, std::wstring(L"Calibri18.spritefont")); m_pSprite->Initialize(d3dDevice_, std::wstring(L"Test.dds"), 10.0f, 10.0f); m_textToDisplay = Text(); m_textToDisplay.Initialize(std::wstring(L"Sarah"), DirectX::SimpleMath::Vector2(200.0f, 200.0f)); try { MSXML2::IXMLDOMDocument2Ptr xmlDoc; HRESULT hr = xmlDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60), NULL, CLSCTX_INPROC_SERVER); teamStats.InitializeTeam("Players.xml"); // Make sure the file was loaded correctly before trying to load the players. if (xmlDoc->load("Players.xml") == VARIANT_TRUE) { //xmlDoc->setProperty("SelectionLanguage", "XPath"); //teamStats.LoadPlayers(xmlDoc); } if (xmlDoc->load("input.xml") != VARIANT_TRUE) { DXTRACE_MSG("Unable to load input.xml\n"); xmlDoc->save("input.xml"); //xmlDoc-> } else { DXTRACE_MSG("XML was successfully loaded \n"); xmlDoc->setProperty("SelectionLanguage", "XPath"); MSXML2::IXMLDOMNodeListPtr wheels = xmlDoc->selectNodes("/Car/Wheels/*"); DXTRACE_MSG("Car has %u wheels\n", wheels->Getlength()); DXTRACE_MSG(wheels->Getitem(0)->text); //ptr-> MSXML2::IXMLDOMNodePtr node; node = xmlDoc->createNode(MSXML2::NODE_ELEMENT, ("Engine"), ("")); node->text = ("Engine 1.0"); xmlDoc->documentElement->appendChild(node); hr = xmlDoc->save("output.xml"); if (SUCCEEDED(hr)) { DXTRACE_MSG("output.xml successfully saved\n"); } } } catch (_com_error &e) { DXTRACE_MSG(e.ErrorMessage()); } return true; }