Click here to Skip to main content
13,400,969 members (37,873 online)
Click here to Skip to main content

Stats

28.4K views
463 downloads
28 bookmarked
Posted 12 Apr 2016

Linq-To-XML Style of Node Creation for C++

, 12 Apr 2016
Linq-To-XML Node Creation for Native C++
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Tryout
res
Tryout.ico
Tryout.vcxproj.user
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
TryoutNet
Properties
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
#include "StdAfx.h"
#include "NewTreeNode.h"

using namespace Elmax;

NewTreeNode::NewTreeNode(void)
{
	pName = L"";
	pValue = L"";

	xmltype = XML_NONE;
}


NewTreeNode::~NewTreeNode(void)
{
}

void NewTreeNode::Delete()
{
	for(size_t i=0;i<vec.size();++i)
		vec.at(i)->Delete();

	vec.clear();
	delete this;
}

bool NewTreeNode::Traverse(MSXML2::IXMLDOMDocumentPtr& ptrDoc,  MSXML2::IXMLDOMNodePtr& parent, NewTreeNode* pNode)
{
	if(!ptrDoc||!parent)
		return false;

	if(pNode==NULL)
		return false;

	for(size_t i=0;i<pNode->vec.size();++i)
	{
		NewTreeNode* pNode1 = pNode->vec.at(i);

		if(pNode1->xmltype==XML_ELEMENT)
		{
			_bstr_t str(pNode1->pName.c_str());
			_variant_t varType(MSXML2::NODE_ELEMENT);
			_bstr_t emptyUri(L"");
			MSXML2::IXMLDOMNodePtr elem = ptrDoc->createNode(varType, str, emptyUri);
			parent->appendChild(elem);
			if(pNode1->pValue!=L"")
				elem->Puttext(_bstr_t(pNode1->pValue.c_str()));

			bool b = Traverse(ptrDoc, elem, pNode1);
			if(b==false)
				return false;
		}
		else if(pNode1->xmltype==XML_COMMENT)
		{
			MSXML2::IXMLDOMNodePtr domComment = ptrDoc->createNode(MSXML2::NODE_COMMENT, _bstr_t(pNode1->pName.c_str()), L"");

			if(!domComment)
				return false;

			domComment->Puttext(pNode1->pValue.c_str());

			parent->appendChild(domComment);
		}
		else if(pNode1->xmltype==XML_CDATA)
		{
			MSXML2::IXMLDOMNodePtr domCData = ptrDoc->createNode(MSXML2::NODE_CDATA_SECTION, _bstr_t(pNode1->pValue.c_str()), L"");

			if(!domCData)
				return false;

			domCData->nodeValue = bstr_t(pNode1->pValue.c_str());

			parent->appendChild(domCData);
		}
		else if(pNode1->xmltype==XML_ATTRIBUTE)
		{
			MSXML2::IXMLDOMNamedNodeMapPtr attrList = parent->Getattributes();

			if(!attrList)
				return false;

			MSXML2::IXMLDOMAttributePtr pAttr = ptrDoc->createAttribute(_bstr_t(pNode1->pName.c_str()));

			if(attrList&&pAttr)
			{
				pAttr->Puttext(_bstr_t(pNode1->pValue.c_str()));
				attrList->setNamedItem(pAttr);
			}
		}
	}
	return true;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Cake Processor
Software Developer (Senior)
United States United States
C++ developer transitioning to Python

Right now, I am picking up DevOps skills at Pluralsight and pursuing CCNA certification. Stay tuned for my CCNA related article!

Lean Programmer Blog

IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • Certified Secure Software Lifecycle Professional (CSSLP)

View my certificates here.

I just started a moral theory blog based on Yinyang ☯ elements. Hop over to read Origins of Yinyang Theory

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180218.2 | Last Updated 12 Apr 2016
Article Copyright 2016 by Cake Processor
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid