article

How to Use DOM and XML in an MFC VC++ App

Email
Submitted on: 1/4/2015 1:30:00 AM
By: davepamn (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: Microsoft Visual C++
Views: 1659
 
     Overview: This article explains how to program Visual C++ to use DOM for XML manipulation. XML is composed of elements. An element is a user defined tag name with a opening and close pair: such as, Attributes are name/value pair combinations within an element. The Document Object Model (DOM) is used to add, update, and remove elements and attributes. This article will explain how to manipulate xml elements and attributes using Visual C++ and DOM.

 
				Visual C++ and XML

Author: David Nishimoto
email: davepamn@relia.net
Download Sample
Overview: This article explains how to program Visual C++ to use DOM and
xml.
XML is composed of elements.An element is a user defined tag name with
a opening and close pair: such as, <Name> </Name>Attributes are name/value
pair combinations within an element. <Name first_name=David last_name=Nishimoto> </Name>
The Document Object Model (DOM) is used to add, update, and remove elements and attributes. This
article will explain how to manipulate xml elements and attributes using Visual C++.

CXMLEngine Class xmlengine.h

1. The DOM object is a pointer which the implementation instantiates
2. The XML character set support unicode code points
class CXMLEngine
{
public:
	IXMLDOMDocumentPtr objDOMDoc;
	BSTR mXML;
public:
	CXMLEngine();
	~CXMLEngine();
	int CXMLEngine::Initialize(char *sFileName);
	IXMLDOMNodePtr GetNode(BSTR sKey);
	HRESULT DeleteNode(BSTR sKey);
	VARIANT GetAttribute(IXMLDOMNodePtr oNode,char *sKey);
	HRESULT SaveXML_To_File(char *filename);
	HRESULT AddNode(char *sParent_Key, char *sKey, char *sElementInformation);
	//HRESULT SetAttribute(IXMLDOMElement *oNode,char *sAttributeName, char *sAttributeValue);
	void Refresh();
};

The DOM COM object is instantiated xmlengine.cpp

1. The DOM COM object instance is instantiated
2. A XML file is loaded into the DOM object
int CXMLEngine::Initialize(char *sFileName)
{
	char searchPath[200];
	try 
{
		
		EVAL_HR(CoInitialize(NULL)); 
		//EVAL_HR(objDOMDoc.CreateInstance("Msxml2.DOMDocument.3.0"));
		EVAL_HR(objDOMDoc.CreateInstance("microsoft.xmldom"));
		
		GetCurrentDirectory(200, searchPath);
		strcat(searchPath,"\\");
		strcat(searchPath,sFileName);
		_variant_t varXml(searchPath);
		_variant_t varOut((bool)TRUE);
		objDOMDoc->async = false;
		varOut = objDOMDoc->load(varXml);
		mXML=objDOMDoc->xml;
		if ((bool)varOut == FALSE)
		 throw(0);
		
		return 0;
	}
	
	catch(...)
	{
		AfxMessageBox("Exception occurred");
		return -1;
	}
	CoUninitialize();
}

Element Manipulation

IXMLDOMNodePtr CXMLEngine::GetNode(BSTR sKey)
{
/*
Purpose:locate a specific node in the
xml structure by its "key" value
*/
	CString sCriteria;
IXMLDOMNodePtr oNode;
sCriteria = "//node[@key $eq$ '";
	sCriteria += sKey;
	sCriteria += "']";
	oNode=objDOMDoc->selectSingleNode(_bstr_t(sCriteria));
if (oNode!=NULL)
	{
return(oNode);
	}
	else
	{
		AfxMessageBox("Node Not Found");
		return(NULL);
	}
}
HRESULT CXMLEngine::DeleteNode(BSTR sKey)
{
/*
'Purpose: remove a node from the xml tree
'1)Find the node
'2)Find the node's parent
'3)Invoke the parents removeChild method deleting
'the node and its descendants from the xml tree.
*/
IXMLDOMNodePtr oNode;
IXMLDOMNodePtr oParentNode;
VARIANT sParent_Key;
oNode = GetNode(sKey);
if(oNode != NULL)
	{
		sParent_Key=GetAttribute(oNode,"parent_key");
		//AfxMessageBox(_bstr_t(sParent_Key));
oParentNode = GetNode(_bstr_t(sParent_Key));
if (oParentNode != NULL)
		{
oParentNode->removeChild(oNode);
		}
	}
	else
	{
		return(E_FAIL);
	}
	return(S_OK);
}
HRESULT CXMLEngine::AddNode(char *sParent_Key, char *sKey, char *sElementInformation)
{
/*
Purpose: Add a node to the xml tree
1)Search for the parent node
2)Assign attribute values to the new node
3)Insert the new node into the xml tree
*/
//IXMLDOMNodePtr oNewNode;
	IXMLDOMElementPtr oElementNode;
IXMLDOMNodePtr oParentNode, oProposedNode;
	CString sCriteria;
HRESULT hr;
	//1-Check if the Node already exists
sCriteria = "//node[@key $eq$ '";
	sCriteria += sKey;
	sCriteria += "']";
oProposedNode = objDOMDoc->selectSingleNode(_bstr_t(sCriteria));
	if (oProposedNode!=NULL)
	{
		return (E_FAIL);
	}
//2-Search for Parent Node Match
sCriteria = "//node[@key $eq$ '";
	sCriteria += sParent_Key;
	sCriteria += "']";
oParentNode = objDOMDoc->selectSingleNode(_bstr_t(sCriteria));
//3-Insert new Node into Document Object Model (dom) structure
if (oParentNode != NULL) 
	{
	//oNewNode = objDOMDoc->createElement("node");
		oElementNode=objDOMDoc->createElement("node");
		hr=oElementNode->setAttribute(_bstr_t("parent_key"),_bstr_t(sParent_Key));
		hr=oElementNode->setAttribute(_bstr_t("key"),_bstr_t(sKey));
 		hr=oElementNode->setAttribute(_bstr_t("element_inforamtion"),_bstr_t(sElementInformation));
		oParentNode->appendChild(oElementNode);
}
	else
	{
		return(E_FAIL);
	}
	return(S_OK);
}

Attribute Manipulation

VARIANT CXMLEngine::GetAttribute(IXMLDOMNodePtr oNode,char *sKey)
{
	IXMLDOMNodePtr oAttribute;
	IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;
	VARIANT sReturn_Key;
	DOMNamedNodeMapPtr = oNode->attributes;
	oAttribute=DOMNamedNodeMapPtr->getNamedItem(_bstr_t(sKey));
	oAttribute->get_nodeValue(&sReturn_Key);
	return(sReturn_Key);
}
Setting an Attribute inside of an element
	hr=oElementNode->setAttribute(_bstr_t("parent_key"),_bstr_t(sParent_Key));		
	hr=oElementNode->setAttribute(_bstr_t("key"),_bstr_t(sKey));
 	hr=oElementNode->setAttribute(_bstr_t("element_inforamtion"),_bstr_t(sElementInformation));


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.