Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual C++ 10.0

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

Rate me:
Please Sign up or sign in to vote.
4.78/5 (14 votes)
12 Apr 2016Ms-PL11 min read 43.5K   501   29  
Linq-To-XML Node Creation for Native C++
#include "StdAfx.h"
#include "HyperElement.h"
#include <cctype>

using namespace Elmax;

HyperElement::HyperElement(void)
{
}


HyperElement::~HyperElement(void)
{
}

std::vector< std::pair<Elmax::Element, Elmax::Element> >
	HyperElement::JoinOneToOne(
	std::vector<Elmax::Element>& vecElem1,
	const std::wstring& attrName1,
	std::vector<Elmax::Element>& vecElem2,
	const std::wstring& attrName2,
	bool bCaseSensitive)
{
	std::wstring str1, str2;
	std::vector< std::pair<Elmax::Element, Elmax::Element> > vecResults;
	for(size_t i=0; i<vecElem1.size(); ++i)
	{
		if(attrName1==L"")
			str1 = vecElem1[i].GetString(L"");
		else
			str1 = vecElem1[i].Attribute(attrName1).GetString(L"");

		for(size_t j=0; j<vecElem2.size(); ++j)
		{
			if(attrName2==L"")
				str2 = vecElem2[j].GetString(L"");
			else
				str2 = vecElem2[j].Attribute(attrName2).GetString(L"");

			if(bCaseSensitive)
			{
				if(str1==str2)
				{
					vecResults.push_back(std::make_pair<Elmax::Element, Elmax::Element>(vecElem1[i], vecElem2[j]));
					break;
				}
			}
			else
			{
				if(ToLowerCase(str1)==ToLowerCase(str2))
				{
					vecResults.push_back(std::make_pair<Elmax::Element, Elmax::Element>(vecElem1[i], vecElem2[j]));
					break;
				}
			}
		}

	}

	return vecResults;
}

std::vector< std::pair<Elmax::Element, std::vector<Elmax::Element> > >
	HyperElement::JoinOneToMany(
	std::vector<Elmax::Element>& vecElem1,
	const std::wstring& attrName1,
	std::vector<Elmax::Element>& vecElem2,
	const std::wstring& attrName2,
	bool bCaseSensitive)
{
	std::wstring str1, str2;
	std::vector< std::pair<Elmax::Element, std::vector<Elmax::Element> > > vecResults;
	bool makepair=false;
	for(size_t i=0; i<vecElem1.size(); ++i)
	{
		makepair=false;
		if(attrName1==L"")
			str1 = vecElem1[i].GetString(L"");
		else
			str1 = vecElem1[i].Attribute(attrName1).GetString(L"");

		for(size_t j=0; j<vecElem2.size(); ++j)
		{
			if(attrName2==L"")
				str2 = vecElem2[j].GetString(L"");
			else
				str2 = vecElem2[j].Attribute(attrName2).GetString(L"");

			if(bCaseSensitive)
			{
				if(str1==str2)
				{
					if(makepair==false)
					{
						std::vector<Elmax::Element> vecChild;
						vecResults.push_back(std::make_pair<Elmax::Element, std::vector<Elmax::Element> >(vecElem1[i], vecChild));
						makepair=true;
					}
					vecResults[vecResults.size()-1].second.push_back(vecElem2[j]);
				}
			}
			else
			{
				if(ToLowerCase(str1)==ToLowerCase(str2))
				{
					if(makepair==false)
					{
						std::vector<Elmax::Element> vecChild;
						vecResults.push_back(std::make_pair<Elmax::Element, std::vector<Elmax::Element> >(vecElem1[i], vecChild));
						makepair=true;
					}
					vecResults[vecResults.size()-1].second.push_back(vecElem2[j]);
				}
			}
		}

	}

	return vecResults;
}

std::wstring HyperElement::ToLowerCase(std::wstring &str)
{
	const int length = str.length();
	std::wstring strDest = str;
	for(int i=0; i < length; ++i)
	{
		strDest[i] = std::tolower(str[i]);
	}

	return strDest;
}

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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions