【XMLDOM】 IXMLDOMComment/insertBefore 应用

HRESULT hr = m_pDoc.CreateInstance("Microsoft.XmlDom");
	if (FAILED(hr)){
		return;
	}

	
	VARIANT_BOOL vb;
	hr = m_pDoc->load(CComVariant(_T("Practice.xml")), &vb);
	if (FAILED(hr) || VARIANT_FALSE == vb){
		return;
	}

#if 0	//document 添加注释节点
	{
		IXMLDOMCommentPtr pComment;
		CComBSTR bstrData(_T("20140628"));
		hr = m_pDoc->createComment(bstrData, &pComment);
		if (FAILED(hr))
			return;

		CComBSTR bstrCommentText(_T("Work overtime"));
		pComment->put_text(bstrCommentText);

		//document 追加子节点
		IXMLDOMNodePtr pOutNewNode;
		hr = m_pDoc->appendChild(pComment, &pOutNewNode);
		if (FAILED(hr))
			return;

		CComBSTR bstrName;
		CComVariant cvValue;
		pOutNewNode->get_nodeName(&bstrName);//#comment
		pOutNewNode->get_nodeValue(&cvValue);//Work overtime
	}
#endif

#if 0//元素节点添加注释节点
	{
		IXMLDOMNodeListPtr pAuthorList;
		hr = m_pDoc->getElementsByTagName(CComBSTR("author"), &pAuthorList);

		IXMLDOMNodePtr pAuthor;
		hr = pAuthorList->get_item(1, &pAuthor);

		VARIANT vLocation;
		VariantInit(&vLocation);
		vLocation.vt = VT_DISPATCH;
		vLocation.pdispVal = pAuthor.GetInterfacePtr();

		IXMLDOMNodePtr pParent;
		hr = pAuthor->get_parentNode(&pParent);

		IXMLDOMCommentPtr pComment2;
		m_pDoc->createComment(CComBSTR("Insert Comment"), &pComment2);

		IXMLDOMNodePtr pOutNewNode2;
		hr = pParent->insertBefore(pComment2, vLocation, &pOutNewNode2);
	}
#endif

#if 0	//selectSingleNode 应用
		IXMLDOMNodePtr pNodeSelect;
		hr = m_pDoc->selectSingleNode(CComBSTR("./catalog/book"), &pNodeSelect);

		BSTR bstrName;
		pNodeSelect->get_nodeName(&bstrName);		//book
		SysFreeString(bstrName);

		pNodeSelect->get_text(&bstrName);		//Gambardella, Matthew XML Developer's Guide Computer 44.95 2000-10-01 An in-depth look at creating applications with XML.
		SysFreeString(bstrName);

		IXMLDOMNodePtr pNodeSelect2, pNodeSelect3;
		pNodeSelect->selectSingleNode(CComBSTR("./price"), &pNodeSelect2);
		pNodeSelect2->get_nodeName(&bstrName);	//price
		SysFreeString(bstrName);

		pNodeSelect2->get_text(&bstrName);	//44.95
		SysFreeString(bstrName);

		pNodeSelect->selectSingleNode(CComBSTR("../book/description"), &pNodeSelect3);
		pNodeSelect3->get_nodeName(&bstrName);	//description
		SysFreeString(bstrName);

		pNodeSelect3->get_text(&bstrName);	//An in-depth look at creating applications with XML.
		SysFreeString(bstrName);
#endif

#if 0	//selectNodes 应用
		IXMLDOMNodeListPtr pBookNodeList;
		hr = m_pDoc->selectNodes(CComBSTR("./catalog/book"), &pBookNodeList);
		
		long lLen;
		pBookNodeList->get_length(&lLen);	//4

		IXMLDOMNodePtr pNode1;
		pBookNodeList->get_item(1, &pNode1);	//

		BSTR bstrName;
		pNode1->get_nodeName(&bstrName);	//book
		SysFreeString(bstrName);

		pNode1->get_text(&bstrName);	//Ralls, Kim Midnight Rain Fantasy 5.95 2000-12-16 A former architect battles corporate zombies, 
															//an evil sorceress, and her own childhood to become queen of the world.
		SysFreeString(bstrName);
#endif
	m_pDoc->save(CComVariant("D:\\Project_Test\\VS2013\\XmlPractice\\XmlPractice\\Practice_new.xml"));

	m_pDoc.Release();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值