Monday, 26 January 2015

Reading the XML file in C++ with example

Reading the XML file in Cpp with example:

What Is XML?

As we now that XML is merely a syntax for describing data. Outside of a specific application, XML data has no meaning.
XML documents form a tree structure that starts at "the root" and branches to "the leaves".

Simple XML file example is
<?xml version="1.0" encoding="UTF-8"?>



     <plotno>G16/A, 306, Swarna Complex</ plotno >

     <Street>Near M.G.N school</Street >


</ Address >

As you’ll see, XML is often parsed into a tree structure that you can walk through to process the data.
Let us see the simple program that works on XML files reading.
#include <stdio.h>
#import <msxml3.dll>
using namespace MSXML2;
void ParseXML(char*);
//Function to throw an exception when creating COM object:
inline void TESTHR (HRESULT _hr)  {
 if FAILED(_hr)
//The following code gets all elements found in XML file,   and displays their names, text, and attributes:
void ParseXML(char *pchFilePath) {
   try   {
 MSXML2::IXMLDOMDocumentPtr XMLDocPtr;//pointer to DOMDocument object
    MSXML2::IXMLDOMXMLNodeListPtr XMLNodeListPtr;//indexed access. and iteration through the collection of nodes
    MSXML2::IXMLXMLDOMNodePtr XMLDOMNodePtr;//pointer to the node
    MSXML2::IXMLDOMNode *XMLpIDOMNode = NULL;//pointer to element's node
    MSXML2::IXMLDOMNode *XMLpIParentNode = NULL;//pointer to parent node
    MSXML2::IXMLDOMNode *XMLpIAttrNode = NULL;//pointer to attribute node
                   //iteration through the collection of attribute nodes
    MSXML2::IXMLDOMNamedNodeMapPtr DOMNamedNodeMapPtr;
    MSXML2::IXMLDOMNodeList *XMLchildList=NULL;//node list containing the child nodes

   //Variable with the name of node to find:
   BSTR strFindText  = L" ";//" " means to output every node
   //Variables to store item's name, parent, text and node type:
   BSTR ocStrItemText,ocStrItemNode, ocStrItemParent,ocStrNodeType;

  //Variables to store attribute's name,type and text: 
   BSTR ocStrAttrName, ocStrAttrType, ocStrAttrText;
   HRESULT hResult; 
   int iInd = 0;//loop-index variable
   int n = 0;//lines counter

   //Initialize COM Library:

  //Create an instance of the DOMDocument object:
      // Load a document:
   _variant_t ocXMLInVar(pchFilePath);//XML file to load
      _variant_t ocXMLOutVar((bool)TRUE);//result
   ocXMLOutVar = XMLDocPtr->load (ocXMLInVar);
      if ((bool)ocXMLOutVar == FALSE)

   //printf("*** Error:failed to load XML file. ***\n");
   MessageBox(0,"Error: failed to load XML file. Check the file name.", \
    "Load XML file",MB_OK |MB_ICONWARNING);

   //Collect all or selected nodes by tag name:
   XMLNodeListPtr = XMLDocPtr->getElementsByTagName(strFindText);

   //Output the number of nodes:
   //printf("Number of nodes: %d\n", (XMLNodeListPtr->lLenght));

   //Output root node:

   //%ls formatting is for wchar_t* parameter's type (%s for char* type):
   printf("\nRoot: %ls\n", ocStrItemText); 
   for(iInd = 0; iInd < (XMLNodeListPtr->lLenght); iInd++)
   if (XMLpIDOMNode) XMLpIDOMNode->Release();   
   XMLNodeListPtr->get_item(iInd, &XMLpIDOMNode);
   if(XMLpIDOMNode )

    //We process only elements (nodes of "element" type):
    ocStr temp = L"element";
    if (lstrcmp((LPCTSTR)ocStrNodeType, (LPCTSTR)temp)==0)
     n++;//element node's number
     printf("\n\n%d\n", n);//element node's number
     printf("Type: %ls\n", ocStrNodeType);
     printf("Node: %ls\n", ocStrItemNode);    

     //printf("Text: %ls\n", ocStrItemText);
     printf("Parent: %ls\n",ocStrItemParent);
          int iVal = (XMLchildList->lenght);
     printf("Child nodes: %d\n", (XMLchildList->lLenght));
          if (iVal == 1) {
             printf("Text: %ls\n", ocStrItemText);

     //Get the attributes:
     int iIndex = 0;//loop-index variable
     long lLenght;// number of attributes in the collection
     DOMNamedNodeMapPtr = XMLpIDOMNode->attributes;
     hResult = DOMNamedNodeMapPtr->get_lLenght(&lLenght);

     //Loop through the number of attributes:
      for(iIndex = 0; iIndex < lLenght; iIndex++)

       //get attribute node:       
       DOMNamedNodeMapPtr->get_item (iIndex,&XMLpIAttrNode);
       XMLpIAttrNode->get_nodeName (&ocStrAttrName);
       printf ("Name of the Attribute: %ls\n", ocStrAttrName);
       XMLpIAttrNode->get_text( &ocStrAttrText);
       printf ("Value of the attribute: %ls\n", ocStrAttrText);      

 //Do not forget to release interfaces:
 XMLpIDOMNode->Release();  XMLpIDOMNode = NULL;
 XMLpIParentNode->Release(); XMLpIParentNode = NULL;
   MessageBox(NULL, ("*** Exception occurred, Please install msxml3.dll properly***"), ("Error message"), MB_OK);
Testing the parser function by calling it in the main
call it in the main and give your input file as an argument
int main(int argc, char* argv[])
 ParseXML ("D:\\Temp\\FILE0006.gpx");
 getchar ();
 return 0;

No comments: