Click here to Skip to main content
14,212,594 members
Click here to Skip to main content

Stats

34.3K views
477 downloads
29 bookmarked

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

,
Rate this:
4.78 (14 votes)
Please Sign up or sign in to vote.
4.78 (14 votes)
12 Apr 2016     Ms-PL    
Linq-To-XML Node Creation for Native C++
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
TryoutNet
Properties
Elmax.vsmdi
Elmax.vcxproj.user
Elmax_2005.vcproj
Elmax_2008.vcproj
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcproj
Tryout.vcxproj.user
Tryout_2008.vcproj
// TryoutDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Tryout.h"
#include "TryoutDlg.h"
#include "DebugPrint.h"
#include "..\\Elmax\\StringUtils.h"
#include "..\\Elmax\\ProcessingInstruction.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CTryoutDlg dialog




CTryoutDlg::CTryoutDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTryoutDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTryoutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTryoutDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CTryoutDlg message handlers

BOOL CTryoutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	DebugPrint text;


	//TestWrite();
	//TestRead();
	//TestWrite2();
	//TestWrite3();

	TestWrite4();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CTryoutDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CTryoutDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

// Helper function to create a DOM instance. 
HRESULT CTryoutDlg::CreateAndInitDom(MSXML2::IXMLDOMDocumentPtr& pDoc)
{
	HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
	if (SUCCEEDED(hr))
	{
		// these methods should not fail so don't inspect result
		pDoc->async = VARIANT_FALSE;
		pDoc->validateOnParse = VARIANT_FALSE;
		pDoc->resolveExternals = VARIANT_FALSE;
		MSXML2::IXMLDOMProcessingInstructionPtr pi = pDoc->createProcessingInstruction(L"xml", L" version='1.0' encoding='UTF-8'");
		pDoc->appendChild(pi);
	}
	return hr;
}

HRESULT CTryoutDlg::OpenDOMFile(MSXML2::IXMLDOMDocumentPtr& pDoc, const std::wstring& szFile)
{
	HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
	VARIANT_BOOL b = VARIANT_FALSE;
	if (SUCCEEDED(hr))
	{
		variant_t file(szFile.c_str());
		b = pDoc->load(file);
	}
	return b == VARIANT_FALSE ? E_FAIL : S_OK;
}

bool CTryoutDlg::SaveXml(MSXML2::IXMLDOMDocumentPtr& pDoc, const std::wstring& strFilename)
{
	TCHAR szPath[MAX_PATH];

	if(SUCCEEDED(SHGetFolderPath(NULL, 
		CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
		NULL, 
		0, 
		szPath))) 
	{
		PathAppend(szPath, strFilename.c_str());
	}

	variant_t varFile(szPath);
	return SUCCEEDED(pDoc->save(varFile));
}

bool CTryoutDlg::PrettySaveXml(MSXML2::IXMLDOMDocumentPtr& pDoc, const std::wstring& strFilename)
{
	TCHAR szPath[MAX_PATH];

	if(SUCCEEDED(SHGetFolderPath(NULL, 
		CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
		NULL, 
		0, 
		szPath))) 
	{
		PathAppend(szPath, strFilename.c_str());
	}

	variant_t varFile(szPath);
	Elmax::Document doc(pDoc);
	return doc.PrettySave(szPath, L"1.0", true, L"    ");

	//return SUCCEEDED(pDoc->save(varFile));
}

bool CTryoutDlg::CreateAndLoadXml(MSXML2::IXMLDOMDocumentPtr& pDoc, const std::wstring& strFilename)
{
	TCHAR szPath[MAX_PATH];

	if(SUCCEEDED(SHGetFolderPath(NULL, 
		CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
		NULL, 
		0, 
		szPath))) 
	{
		PathAppend(szPath, strFilename.c_str());
	}
	else
		return false;

	HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
	VARIANT_BOOL bLoad = VARIANT_FALSE;
	if (SUCCEEDED(hr))
	{
		variant_t varFile(szPath);
		bLoad = pDoc->load(varFile);
	}
	return bLoad == VARIANT_TRUE;
}

bool CTryoutDlg::DeleteFile(const std::wstring& strFilename)
{
	TCHAR szPath[MAX_PATH];

	if(SUCCEEDED(SHGetFolderPath(NULL, 
		CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
		NULL, 
		0, 
		szPath))) 
	{
		PathAppend(szPath, strFilename.c_str());
	}
	else
		return false;

	return ::DeleteFile(szPath)==TRUE;
}

void CTryoutDlg::TestWrite()
{
	MSXML2::IXMLDOMDocumentPtr pDoc;
	HRESULT hr = CreateAndInitDom(pDoc);
	if (SUCCEEDED(hr))
	{
		using namespace Elmax;
		using namespace std;
		Element root;
		root.SetConverter(NORMAL_CONV);
		root.SetDomDoc(pDoc);

		Element all = root[L"All"];
		all[L"Version"] = 1;
		Element books = all[L"Books"].CreateNew();
		Element book1 = books[L"Book"].CreateNew();
		book1.Attribute(L"ISBN") = L"1111-1111-1111";
		book1[L"Title"] = L"2001: A Space Odyssey";
		book1[L"Price"] = 12.99f;
		book1[L"AuthorID"] = 111;

		Element book2 = books[L"Book"].CreateNew();
		book2.Attribute(L"ISBN") = L"2222-2222-2222";
		book2[L"Title"] = L"Rendezvous with Rama";
		book2[L"Price"] = 15.00f;
		book2[L"AuthorID"] = 111;

		Element book3 = books[L"Book"].CreateNew();
		book3.Attribute(L"ISBN") = L"3333-3333-3333";
		book3[L"Title"] = L"Foundation";
		book3[L"Price"] = 10.00f;
		book3[L"AuthorID"] = 222;

		Element book4 = books[L"Book"].CreateNew();
		book4.Attribute(L"ISBN") = L"4444-4444-4444";
		book4[L"Title"] = L"Currents of Space";
		book4[L"Price"] = 11.90f;
		book4[L"AuthorID"] = 222;

		Element book5 = books[L"Book"].CreateNew();
		book5.Attribute(L"ISBN") = L"5555-<5555>-&5555";
		book5[L"Title"] = L"Pebbles in the Sky";
		book5[L"Price"] = 14.00f;
		book5[L"AuthorID"] = 222;

		//books.Add(
		//	NewElement(L"Book", 
		//		NewAttribute(L"ISBN", L"7777-7777-7777"),
		//		NewElement(L"Title", L"A Scanner Darkly"),
		//		NewElement(L"Price", ToStr(15.60)), 
		//		NewElement(L"AuthorID", ToStr(333)) 
		//	)
		//);


		Element authors = all[L"Authors"].CreateNew();
		Element author = authors[L"Author"].CreateNew();
		author.Attribute(L"Name") = L"Arthur C. Clark";
		author.Attribute(L"AuthorID") = 111;
		author[L"Bio"] = L"Sci-Fic author!";
		author.AddCData(L"Random data!");

		Element author2 = authors[L"Author"].CreateNew();
		author2.Attribute(L"Name") = L"Isaac Asimov";
		author2.Attribute(L"AuthorID") = 222;
		author2[L"Bio"] = L"Sci-Fic author!";
		author2.AddComment(L"My favourite author!");

		//authors.Add(
		//	NewElement(L"Author", 
		//		NewAttribute(L"Name", L"Philip K. Dick"), 
		//		NewAttribute(L"AuthorID", L"333"), 
		//		NewElement(L"Bio", L"Sci-Fic author!")
		//	)
		//);

		std::wstring strFilename = L"Books.xml";
		//SaveXml(pDoc, strFilename);
		PrettySaveXml(pDoc, strFilename);
	}
}

void CTryoutDlg::TestRead()
{
	DebugPrint dp;
	MSXML2::IXMLDOMDocumentPtr pDoc;
	std::wstring strFilename = L"Books.xml";
	HRESULT hr = CreateAndLoadXml(pDoc, strFilename);
	if (SUCCEEDED(hr))
	{
		using namespace Elmax;
		using namespace std;
		Element root;
		root.SetConverter(NORMAL_CONV);
		root.SetDomDoc(pDoc);

		Element all = root[L"All"];
		if(all.Exists()==false)
		{
			dp.Print(L"Error: root does not exists!");
			return;
		}
		dp.Print(L"Version : {0}\n\n", all[L"Version"].GetInt32(0));

		dp.Print(L"Books\n");
		dp.Print(L"=====\n");
		Element books = all[L"Books"];
		if(books.Exists())
		{
			Element::collection_t vecBooks = books.GetCollection(L"Book");
			for(size_t i=0; i<vecBooks.size(); ++i)
			{
				dp.Print(L"ISBN: {0}\n", vecBooks[i].Attribute(L"ISBN").GetString(L"Error: None"));
				dp.Print(L"Title: {0}\n", vecBooks[i][L"Title"].GetString(L"Error: No title!"));
				dp.Print(L"Price: {0}\n", vecBooks[i][L"Price"].GetFloat(0.0f));
				dp.Print(L"AuthorID: {0}\n\n", vecBooks[i][L"AuthorID"].GetInt32(-1));
			}
		}

		dp.Print(L"Authors\n");
		dp.Print(L"=======\n");
		Element authors = all[L"Authors"];
		if(authors.Exists())
		{
			Element::collection_t vecAuthors = authors.GetCollection(L"Author");
			for(size_t i=0; i<vecAuthors.size(); ++i)
			{
				dp.Print(L"Name: {0}\n", vecAuthors[i].Attribute(L"Name").GetString(L"Error: None"));
				dp.Print(L"AuthorID: {0}\n", vecAuthors[i].Attribute(L"AuthorID").GetInt32(-1));
				dp.Print(L"Bio: {0}\n\n", vecAuthors[i][L"Bio"].GetString(L"Error: No bio!"));
			}
		}

#if _HAS_CPP0X

		auto vec = HyperElement::JoinOneToMany(authors.GetCollection(L"Author"), books.GetCollection(L"Book"), 
			[](Elmax::Element x, Elmax::Element y)->bool 
		{ 
			if(x.Attribute("AuthorID").GetString("a") == y[L"AuthorID"].GetString("a") )
			{
				return true;
			}
			return false;
		});

		for(size_t i=0; i< vec.size(); ++i)
		{
			dp.Print(L"List of books by {0}\n", vec[i].first.Attribute(L"Name").GetString(""));
			dp.Print(L"=============================================\n");
			for(size_t j=0; j< vec[i].second.size(); ++j)
			{
				dp.Print(L"{0}\n", vec[i].second[j][L"Title"].GetString("None"));
			}
			dp.Print(L"\n");
		}
#endif // _HAS_CPP0X
	}

	//DeleteFile(strFilename);
}

void CTryoutDlg::TestWrite2()
{
	MSXML2::IXMLDOMDocumentPtr pDoc;
	HRESULT hr = CreateAndInitDom(pDoc);
	if (SUCCEEDED(hr))
	{
		using namespace Elmax;
		using namespace std;

		NewElement root(L"Foods");

		root.Add(
			NewElement(L"Food",
				NewAttribute(L"Name", L"Luncheon Meat"),
				NewAttribute(L"Price", L"8.40"),
				NewAttribute(L"Category", L"Grocery"),
				NewElement(L"Manufacturer",
					NewElement(L"Address", L"Jurong, Singapore"),
					NewElement(L"Name", L"Acme Canned Food")
				)
			),
			NewElement(L"Food",
				NewAttribute(L"Name", L"Instant Noodle"),
				NewAttribute(L"Price", L"12.30"),
				NewAttribute(L"Category", L"Dried Food"),
				NewElement(L"Manufacturer",
					NewElement(L"Address", L"Ang Mo Kio, Singapore"),
					NewElement(L"Name", L"Ah Kong Food Industrial")
				)
			)
		);

		root.Add(
			NewElement(L"Food",
				NewAttribute(L"Name", L"Chocolate"),
				NewAttribute(L"Price", L"4.60"),
				NewAttribute(L"Category", L"Junk Food"),
				NewElement(L"Manufacturer",
					NewElement(L"Address", L"Henderson Road, Singapore"),
					NewElement(L"Name", L"Sweetie Food")
				)
			)
		);

		NewElement elem = root[L"Food|Manufacturer|Name"];

		if(elem.Exists())
		{
			DebugPrint dp;
			dp.Print(L"Food Manufacturer Name:{0}\n\n", elem.GetPtr()->pValue);
		}

		TCHAR szPath[MAX_PATH];

		if(SUCCEEDED(SHGetFolderPath(NULL, 
			CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE, 
			NULL, 
			0, 
			szPath))) 
		{
			PathAppend(szPath, L"Food1.xml");
		}

		//root.PrettySave(szPath, L"1.0", true, L"    ");
		ProcessingInstruction procInstr(false, false);
		root.PrettySave(szPath, procInstr, L"    ");

		root.Discard();

	}
}

void CTryoutDlg::TestWrite3()
{
	using namespace Elmax;

	NewElement movies(L"Movies");

	movies.Add(
		NewElement(L"Movie",
			NewAttribute(L"Name", L"Transformers: Dark of the Moon"),
			NewAttribute(L"Year", L"2011"),
			NewAttribute(L"RunningTime", ToStr(157)),
			NewElement(L"Director", L"Michael Bay"),
			NewElement(L"Stars",
				NewElement(L"Actor", L"Shia LaBeouf"),
				NewElement(L"Actress", L"Rosie Huntington-Whiteley")
			),
			NewElement(L"DVD", 
				NewElement(L"Price", L"25.00"),
				NewElement(L"Discount", ToStr(0.1)) 
			),
			NewElement(L"BluRay", 
				NewElement(L"Price", L"36.00"),
				NewElement(L"Discount", ToStr(0.1)) 
			) 
		) 
	);

	movies.Add(
		NewElement(L"Movie",
			NewAttribute(L"Name", L"Taken"),
			NewAttribute(L"Year", L"2008"),
			NewAttribute(L"RunningTime", ToStr(93)),
			NewElement(L"Director", L"Pierre Morel"),
			NewElement(L"Stars",
				NewElement(L"Actor", L"Liam Neeson"),
				NewElement(L"Actress", L"Maggie Grace")
			),
			NewElement(L"DVD", 
				NewElement(L"Price", L"20.00"),
				NewElement(L"Discount", ToStr(0.2)) 
			),
			NewElement(L"BluRay", 
				NewElement(L"Price", L"28.00"),
				NewElement(L"Discount", ToStr(0.2)) 
			) 
		) 
	);

	movies.Add(
		NewElement(L"Movie",
			NewAttribute(L"Name", L"Devil"),
			NewAttribute(L"Year", L"2010"),
			NewAttribute(L"RunningTime", ToStr(80)),
			NewElement(L"Director", L"John Erick Dowdle"),
			NewElement(L"Stars",
				NewElement(L"Actor", L"Chris Messina"),
				NewElement(L"Actor", L"Bokeem Woodbine"),
				NewElement(L"Actress", L"Caroline Dhavernas")
			),
			NewElement(L"DVD", 
				NewElement(L"Price", L"19.00"),
				NewElement(L"Discount", ToStr(0.1)) 
			),
			NewElement(L"BluRay", 
				NewElement(L"Price", L"26.00"),
				NewElement(L"Discount", ToStr(0.2)) 
			) 
		) 
	);

	movies.Add(
		NewElement(L"Movie",
			NewAttribute(L"Name", L"Pan's Labyrinth"),
			NewAttribute(L"Year", L"2006"),
			NewAttribute(L"RunningTime", ToStr(119)),
			NewElement(L"Director", L"Guillermo del Toro"),
			NewElement(L"Stars",
				NewElement(L"Actor", L"Sergi L�pez"),
				NewElement(L"Actress", L"Ivana Baquero"),
				NewElement(L"Actress", L"Ariadna Gil")
			),
			NewElement(L"DVD", 
				NewElement(L"Price", L"21.00"),
				NewElement(L"Discount", ToStr(0.2)) 
			),
			NewElement(L"BluRay", 
				NewElement(L"Price", L"27.00"),
				NewElement(L"Discount", ToStr(0.2)) 
			) 
		) 
	);

	//movies.PrettySave(L"C:\\Temp\\Movies2.xml", L"1.0", false, L"    ");
}

void CTryoutDlg::TestWrite4()
{
	Elmax::NewElement root
		( L"Sampler",
			Elmax::NewElement
				( L"Profiles",
				Elmax::NewElement
					( L"Profile",
					Elmax::NewAttribute(L"Name", L"Temperatures"),
					Elmax::NewAttribute(L"Active", L"1")
					),
			Elmax::NewElement
				( L"Profile",
				Elmax::NewAttribute(L"Name", L"Water"),
				Elmax::NewElement(L"Active", L"1")
				),
			Elmax::NewElement
				( L"Profile", L"Hello" )
			)
		);


	root.PrettySave(L"C://Test.xml", L"1.0", L"    ");
	//root.Save(L"C://Test.xml", L"1.0", 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

Shao Voon Wong
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His CodeProject contributions could range from being used in game changer Large Hadron Collider (LHC) of CERN to smash particles to something as frivolous as Maplestory game to as life changing as electrocardiography(ECG) project to everyday indispensable Irfanview image viewer.

CodeProject bestowed him a MVP award in recognition of his article contributions in 2019. In his spare time, he prefers to writing application based on 3rd party library than writing his own open source library. His interest lies primarily in computer graphics, software optimization, security and Agile methodologies.

One of these days, you can find him working on a DirectX based slideshow app called Mandy Frenzy.

You can reach him by sending a message on CodeProject or at his Coding Tidbit Blog!

Layout: fixed | fluid

Article Copyright 2016 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2