Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 7 Jun 2012 Ms-PL
Linq-To-XML Node Creation for Native C++
elmax-84498.zip
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
Settings.settings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
elmax-ver083.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver084.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver085.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver086.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver087.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
Elmax-ver089.zip
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
Settings.settings
ElmaxSrcVer082.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
#pragma once
#include "Element.h"
#include "UnicodeFile.h"
#include "ProcessingInstruction.h"

namespace Elmax
{
//! helper class for to call dom document's XPath methods
class Document
{
public:
	//! Non-default constructor
	Document(MSXML2::IXMLDOMDocumentPtr ptrDoc) : m_ptrDoc(ptrDoc) {};
	//! Destructors
	~Document(void);

	//! Get a list of elements by their tag name
	//!
	//! @param tagName is the tag name aka element name
	std::vector<Elmax::Element> GetElementsByTagName(const std::wstring& tagName);
	//! Get a list of elements by the XPath expression
	//! 
	//! @param xpath is the XPath expression
	std::vector<Elmax::Element> SelectNodes(const std::wstring& xpath);
	//! Get element by the XPath expression
	//! 
	//! @param xpath is the XPath expression
	Elmax::Element SelectSingleNode(const std::wstring& xpath);

	//! Save with the indentation and newlines
	//!
	//! @param file is file path
	//! @param xmlVersion is the xml version for the processing instruction, eg, L"1.0"
	//! @param utf8 specifies it should be saved in utf-8 or utf-16
	//! @param indent is indentation string used, eg L"\t"
	bool PrettySave(const std::wstring& file, const std::wstring& xmlVersion, bool utf8, const std::wstring& indent = L"    ");

	//! Save with the indentation and newlines
	//!
	//! @param file is file path
	//! @param procIntr specifies the processing instruction for the xml or none
	//! @param indent is indentation string used, eg L"\t"
	bool PrettySave(const std::wstring& file, ProcessingInstruction& procInstruction, const std::wstring& indent = L"    ");

private:
	//! Convert XMLNodeList to a vector of Element
	//! 
	//! @param pList is a smart pointer to IXMLNodeList
	//! @param vec is the returned vector of elements
	void ConvNodeListToVector(MSXML2::IXMLDOMNodeListPtr pList, std::vector<Elmax::Element>& vec);

	//! Write string to unicode file
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies the string should be written in utf-8 or utf-16
	//! @param str is the string to be written.
	static bool Write(CUnicodeFile& uf, bool utf8, const std::wstring& str);

	//! Write newline to unicode file
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies the string should be written in utf-8 or utf-16
	static bool WriteNewLine(CUnicodeFile& uf, bool utf8);

	//! Write indentation to unicode file
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param indent is indentation string used, eg L"\t"
	static bool WriteIndent(CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& indent);

	//! Write start element (eg, <element>) to unicode file
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param elementName is the element name
	//! @param indent is indentation string used, eg L"\t"
	static bool WriteStartElement(CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& elementName, const std::wstring& indent);

	//! Write start element (eg, <element>) to unicode file (without the newline at the beginning)
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param elementName is the element name
	//! @param indent is indentation string used, eg L"\t"
	static bool WriteFirstStartElement(CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& elementName, const std::wstring& indent);

	//! Write end element (eg, </element>) to unicode file
	//!
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param elementName is the element name
	//! @param indent is indentation string used, eg L"\t"
	static bool WriteEndElement(CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& elementName, const std::wstring& indent);

	//! Traverse the tree recursively and write them to file.
	//!
	//! @param node is the xml tree node
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param indent is indentation string used, eg L"\t"
	static bool Traverse(MSXML2::IXMLDOMNodePtr& node, CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& indent);

	//! Traverse the tree recursively and write them to file (without the processing instruction)(for pretty save)
	//!
	//! @param node is the xml tree node
	//! @param uf is the opened unicode file
	//! @param utf8 specifies it should be written in utf-8 or utf-16
	//! @param cnt is the number of indentation should be written
	//! @param indent is indentation string used, eg L"\t"
	static bool TraverseWithoutProcessingInstruction(MSXML2::IXMLDOMNodePtr& node, CUnicodeFile& uf, bool utf8, size_t cnt, const std::wstring& indent);

	//! Replace all the anchor string instance found in the fmtstr string with another string
	static std::wstring Replace( 
		const std::wstring& fmtstr, 
		const std::wstring& anchor,
		const std::wstring& replace );

	//! Escape the XML string(eg, L"<>" to L"&lt;&gt;"
	static std::wstring EscapeXML( 
		const std::wstring& fmtstr);

	//! Unescape the XML string(eg, L"&lt;&gt;" to L"<>"
	static std::wstring UnEscapeXML( 
		const std::wstring& fmtstr);


	//! Internal XML Document
	MSXML2::IXMLDOMDocumentPtr m_ptrDoc;
};


}

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
Singapore Singapore
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Jun 2012
Article Copyright 2011 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid