Click here to Skip to main content
11,633,425 members (76,931 online)
Click here to Skip to main content
Add your own
alternative version

Import/Export registry sections as XML

, 21 Jan 2003 114K 4.6K 70
Export registry sections as XML to simplify registry diffs
registryasxml_demo.zip
ReleaseMFCdll
registryasxml.exe
Release
registryasxml.exe
registryasxml_src.zip
registryxml.dsp
registryxml.dsw
res
Icon_Anchor.jpg
Icon_Form.jpg
Icon_Frame.jpg
Icon_Media.jpg
Icon_Root.jpg
ImageList.bmp
ImageList256.bmp
registryxml.ico
RegistryxmlDoc.ico
TocImageList.bmp
Toolbar.bmp
registryxml.opt
registryxml.plg
registryxml.suo
registryasxml__demo.zip
registryasxml.exe
registryasxml.exe
registryasxml__src.zip
registryxml.dsp
registryxml.dsw
Icon_Anchor.jpg
Icon_Form.jpg
Icon_Frame.jpg
Icon_Media.jpg
Icon_Root.jpg
ImageList.bmp
ImageList256.bmp
registryxml.ico
RegistryxmlDoc.ico
TocImageList.bmp
Toolbar.bmp
#include "stdafx.h"
#include "xmlwriter.h"


XmlWriter::XmlWriter()
{
	m_pFile = new CFile();
	Init();
}
XmlWriter::~XmlWriter()
{
	delete m_pFile;
	m_pFile = NULL;
}
void XmlWriter::Init()
{
	SetIndentLevel(0);
	m_bFileOpen = FALSE;
	m_bPrototypeWritten = FALSE;
}
BOOL XmlWriter::Open(CString &szFilename)
{
	if (m_bFileOpen)
		return TRUE;

	m_szFilename = szFilename;

	return TRUE;
}
BOOL XmlWriter::WriteString(CString &szData)
{
	if (!m_bFileOpen) // open file
	{
		m_bFileOpen = m_pFile->Open( (LPCTSTR)m_szFilename, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone );
	}
	if (!m_bFileOpen)
		return FALSE;

	if (!m_bPrototypeWritten)
	{
		m_bPrototypeWritten = TRUE; // make sure to set this flag to true before we call a reentrant method such like .WritePInstruction

		// write XML prototype, once and only once
		//
		int nOldIndent = GetIndentLevel();
		SetIndentLevel(0);

		CString s;
		XmlElement xml("xml");
		xml.AddAttrib( CString("version"), CString("1.0") );
		xml.AddAttrib( CString("encoding"), CString("UTF-8") );
		xml.WritePInstruction(*this);
		
		SetIndentLevel(nOldIndent);

	}

	// actual write
//	m_pFile->SeekToEnd();
	m_pFile->Write( szData.GetBuffer(0), szData.GetLength() );
//	m_pFile->Flush();

	return TRUE;
}
BOOL XmlWriter::Close()
{
	if (m_pFile && m_bFileOpen)
	{
		m_pFile->Close();
	}
	Init();
	return TRUE;
}



XmlStringWriter::XmlStringWriter()
{
	Init();
}
XmlStringWriter::~XmlStringWriter()
{
}
void XmlStringWriter::Init()
{
	m_szContent.Empty();
}
CString &XmlStringWriter::GetContent()
{
	return m_szContent;
}
BOOL XmlStringWriter::WriteString(CString &szData)
{
	if (!m_bPrototypeWritten)
	{
		m_bPrototypeWritten = TRUE; // make sure to set this flag to true before we call a reentrant method such like .WritePInstruction

		// write XML prototype, once and only once
		//
		int nOldIndent = GetIndentLevel();
		SetIndentLevel(0);

		CString s;
		XmlElement xml("xml");
		xml.AddAttrib( CString("version"), CString("1.0") );
		xml.AddAttrib( CString("encoding"), CString("UTF-8") );
		xml.WritePInstruction(*this);
		
		SetIndentLevel(nOldIndent);

	}

	// actual write
	m_szContent += szData;

	return TRUE;
}
BOOL XmlStringWriter::Close()
{
	return TRUE;
}




void HelperBuildIndent(CString &szSpaces, int nLevel)
{
	szSpaces.Empty();
	for (int i=0;i<nLevel;i++)
		szSpaces += " ";
}

XmlElement::XmlElement()
{
}
XmlElement::XmlElement(const CString &szName)
{
	SetName(szName);
}
void XmlElement::SetName(/*in*/const CString &szName)
{
	m_szName = szName;
}
void XmlElement::GetName(/*out*/CString &szName)
{
	szName = m_szName;
}
void XmlElement::GetAttribName(int nIndex, /*out*/ CString &szAttribName)
{
	szAttribName.Empty();
	if (nIndex>-1 && nIndex<GetAttribCount())
		szAttribName = m_szAttribNames.GetAt( nIndex );
}
void XmlElement::GetAttribValue(int nIndex, /*out*/ CString &szAttribValue)
{
	szAttribValue.Empty();
	if (nIndex>-1 && nIndex<m_szAttribValues.GetSize())
		szAttribValue = m_szAttribValues.GetAt( nIndex );
}
int XmlElement::GetAttribCount()
{
	return (int) m_szAttribNames.GetSize();
}
BOOL XmlElement::FindAttrib(/*in*/CString &szAttribName)
{
	BOOL bFound = FALSE;
	int i=0;
	int nSize = (int) m_szAttribNames.GetSize();
	while (i<nSize && !bFound)
	{
		bFound = (m_szAttribNames.GetAt(i)==szAttribName);
		i++;
	}
	return bFound;
}
void XmlElement::AddAttrib(/*in*/CString &szAttribName, /*in*/CString &szAttribValue)
{
	BOOL bFound = FALSE;
	int i=0;
	int nSize = (int) m_szAttribNames.GetSize();
	while (i<nSize && !bFound)
	{
		bFound = (m_szAttribNames.GetAt(i)==szAttribName);
		i++;
	}
	if (bFound) // already known
	{
		i--;
		m_szAttribValues.SetAt( i, szAttribValue );
	}
	else
	{
		m_szAttribNames.Add( szAttribName );
		m_szAttribValues.Add( szAttribValue );
	}
}
void XmlElement::Write(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL) // for any kind of open tag
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += ">";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );
}
void XmlElement::WriteEmpty(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL)
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += "/>";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );

	writer.SetIndentLevel( writer.GetIndentLevel()-nDeltaLevel );
}
void XmlElement::WritePInstruction(XmlWriter &writer, int nDeltaLevel)
{
	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );

	CString s;
	HelperBuildIndent(s,writer.GetIndentLevel());
	s += "<?";
	s += m_szName;
	int i;
	int nCount = GetAttribCount();
	for (i=0;i<nCount;i++)
	{
		s += " "; // separator between attribute pairs
		s += m_szAttribNames.GetAt( i );
		s += "=\"";
		s += m_szAttribValues.GetAt( i );
		s += "\"";
	}
	s += "?>";
	s += "\r\n"; // ENDL
	writer.WriteString( s );
}
void XmlElement::WriteClosingTag(XmlWriter &writer, int nDeltaLevel, BOOL bIndent, BOOL bEOL)
{
	CString s;
	if (bIndent)
		HelperBuildIndent(s,writer.GetIndentLevel());
	s += "</";
	s += m_szName;
	s += ">";
	if (bEOL)
		s += "\r\n"; // ENDL
	writer.WriteString( s );

	writer.SetIndentLevel( writer.GetIndentLevel()+nDeltaLevel );
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 22 Jan 2003
Article Copyright 2002 by Stephane Rodriguez.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid