Click here to Skip to main content
15,888,170 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a CString which contains the XML contents in it . I have to append this to an existing XML file. How do I proceed?

I tried to do it this way:
C++
IXMLDOMDocument2Ptr pXMLDoc;
//load the below xml with the above ptr
XML 1:
<root>
  <root1>
   ..
   ..
  </root1>
</root>


C++
<hr = pXMLDRSDoc.CreateInstance(__uuidof(MSXML2::DOMDocument40));
CString strXML = _T("<A><C>xmlContent</C></A>");

//Select the node under which u want to place the above XML string XML
MSXML2::IXMLDOMNodePtr pNodePtr;
pNodePtr = pXMLDoc->selectSingleNode(_T("//root/root1"));
pXMLDoc->appendChild(pNodePtr);


But this crashes. Access violation.

Can somebody pls help me with this?

Expected res is as below:
C++
IXMLDOMDocument2Ptr pXMLDoc;
//load the below xml with the above ptr
XML 1:
<root>
  <root1>
     <A>
        <C>xmlContent</C>
     </A>
  </root1>
</root>


Regards
Posted
Comments
Ehsan A Samani 6-Nov-13 13:34pm    
where is the actual part where you load the xml file from disk?
also why is that your XPath has looks like this "//root/root1"?

aren't you interested in only one root1 element?

so shouldn't be something more like "/root/root1[0]"?

1 solution

C++
bool GetInlineXmlDoc(LPCTSTR pcszInlineXml,CComPtr<ixmldomdocument> & inlineXmlDoc)
{
	if (SUCCEEDED(::CoCreateInstance(CLSID_DOMDocument2, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&inlineXmlDoc)))
	{
		VARIANT_BOOL isSuccessful = FALSE;
		if(SUCCEEDED(inlineXmlDoc->loadXML(_bstr_t(pcszInlineXml), &isSuccessful)))
		{
			return true;
		}
	}	
	return false;
}</ixmldomdocument>


C++
bool GetDocumentRootChild(CComPtr<ixmldomdocument> xmlDoc,CComPtr<ixmldomnode>& rootNode)
{
	if (SUCCEEDED(xmlDoc->get_firstChild(&rootNode)))
	{
		CComBSTR bstr;
		if (SUCCEEDED(rootNode->get_nodeName(&bstr)))
		{
			//testing node name
		}
		return true;
	}
	return false;
}</ixmldomnode></ixmldomdocument>


C++
int _tmain(int argc, _TCHAR* argv[])
{	LPCTSTR pcszInlineXml =  _T("<a><c>xmlContent</c></a>");
	HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
	if (SUCCEEDED(hr))
	{
		CComPtr<ixmldomdocument> XMLDoc;
		// Check the return value, hr...
		HRESULT hr2 = ::CoCreateInstance(CLSID_DOMDocument2, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&XMLDoc);
		if (SUCCEEDED(hr2))
		{			
			VARIANT varFileName;
			//varFileName.bstrVal = _bstr_t(_T("c:\\test\\testmsxml\\release\\rootXmlFile.xml"));
			varFileName.bstrVal = _bstr_t(_T("rootXmlFile.xml"));
			varFileName.vt = VT_BSTR;
			VARIANT_BOOL isSuccess = FALSE;
			if (SUCCEEDED(XMLDoc->load(varFileName, &isSuccess)))
			{	
				CComPtr<ixmldomdocument> inlineXmlDoc;
				if (GetInlineXmlDoc(pcszInlineXml,inlineXmlDoc))
				{
					CComPtr<ixmldomnode> rootNodeofInline;
					if (GetDocumentRootChild(inlineXmlDoc, rootNodeofInline))
					{					
						CComPtr<ixmldomnode> node;
						if (SUCCEEDED(XMLDoc->selectSingleNode(_bstr_t(_T("/root/root1[0]")), &node)))
						{
							CComPtr<ixmldomnode> newChild;
							if (SUCCEEDED(node->appendChild(rootNodeofInline, &newChild)))
							{
								if (SUCCEEDED(XMLDoc->save(varFileName)))
								{
									::MessageBox(NULL, _T("Worked"), _T("Worked"), MB_OK);
								}
							}							
						}
					}
				}
				
			}
			::VariantClear(&varFileName);
		}		
	}
	::CoUninitialize();
	
	return 0;
}</ixmldomnode></ixmldomnode></ixmldomnode></ixmldomdocument></ixmldomdocument>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900