如何使用MSXML C ++合并2个XML文件 [英] How to merge 2 XML files using MSXML C++

查看:91
本文介绍了如何使用MSXML C ++合并2个XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个CString,其中包含XML内容。我必须将它附加到现有的XML文件。我该怎么办?



我试着这样做:

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:

IXMLDOMDocument2Ptr pXMLDoc;
//load the below xml with the above ptr
XML 1:
<root>
  <root1>
   ..
   ..
  </root1>
</root>







<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:

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





关注



Regards

推荐答案

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>







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>







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>


这篇关于如何使用MSXML C ++合并2个XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆