eXtensible Markup Language - XML File Access

 XML File Access:
An XML (eXtensible Markup Language) file is a text-based file format used for storing and transporting data. It is a markup language, similar to HTML, but with a key difference: while HTML uses predefined tags to display data, XML allows users to define their own tags and document structure to describe the data itself.

XML documents can be created in two different ways:
a) With user defined tags
b) With a template (xslt files)

There are different parsers available to parse an XML file:
e.g.
a) DOM [Document Object Model]
b) SAX

1) The parsers provide different API's to extract data from the XML File.
2) XML File is read as 'document' by the parser.
3) Each tags should have an opening and closing tag, that is called a Node.
4) A Node can have attributes and inner text.
5) Both Attributes and inner text provides data about the node.

Example:
<?xml version="1.0" encoding="utf-8"?>
<STUDENT_RECORDS>
        <stu_rec>
                <stu_name>Apple</stu_name>
                <stu_id>10</stu_id>
                <stu_course>MCA</stu_course>
        </stu_rec>
        <stu_rec>
              <stu_name>Banana</stu_name>
              <stu_id>20</stu_id>
              <stu_course>MCA</stu_course>
        </stu_rec>
        <stu_rec>
              <stu_name>Cashew</stu_name>
              <stu_id>3</stu_id>
              <stu_course>MCA</stu_course>
         </stu_rec>
</STUDENT_RECORDS>  

Accessing XML File:
(i) C++ FStream Access:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
        char line[100];
  char StudentName[100];
  int StudentID;
  char StudentCourse[100];

fstream fs;
fs.open("XMLFile.xml", ios::in);

fs >> line;
while (strlen(line) != 0)
{
if (strcmp(line, "<stu_rec>") == 0)
{
fs >> line;
cout << "STUDENT RECORD:" << endl;
while (strcmp(line, "</stu_rec>") != 0)
{
char* res = strchr(line, '>');
int f = res - line;
char* res1 = strrchr(line, '<');
int l = res1 - line;
char str[100];
int len = strlen(line);
strcpy(str, line);
for (int i = f+1; i < l; i++)
{
cout << str[i];
}
cout << endl;
fs >> line;
}
cout << endl;
}
fs >> line;
}

return 0;
}

(ii) XML DOM Access (Document Object Model):
Enables to programmatically load and parse an XML File and can be used to traverse XML data.

XMLDOMDocument -> Top Node of the XML document
XMLDOMNode -> Any single node in the XML document tree
XMLDOMNodeList -> Collection of all XMLDOMNode objects
XMLDOMNamedNodeMap -> Collection of all XML document tree attributes

enum tagDOMNodeType
{
NODE_INVALID,
NODE_ELEMENT,
NODE_ATTRIBUTE,
NODE_TEXT,
NODE_CDATA_SECTION,
NODE_ENTITY_REFERENCE,
NODE_ENTITY,
NODE_PROCESSING_INSTRUCTION,
NODE_COMMENT,
NODE_DOCUMENT,
NODE_DOCUMENT_TYPE,
NODE_DOCUMENT_FRAGMENT,
NODE_NOTATION
};

Example:
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <objbase.h>
#include <atlbase.h>
#include <atlcom.h>

//#include <msxml.h>
//#include <msxml6.h>

#import "msxml3.dll" named_guids raw_interfaces_only
//using namespace MSXML2;

#include <iostream>
using namespace std;

int main()
{
        HRESULT hr;

        cout << "main - start" << endl;

        hr = ::CoInitialize(NULL);
        if (hr != S_OK)
        {
        cout << "CoInitialize Failed" << endl;
        cout << hr << endl;
        return -1;
        }

        IXMLDOMDocument* pIXMLDOMDocument = NULL;
        hr = CoCreateInstance(CLSID_DOMDocument, NULL,                 CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (LPVOID*)(&pIXMLDOMDocument));
        if (hr != S_OK)
        {
        cout << "CoCreateInstance - IID_IXMLDOMDocument Failed" << endl;
        return -1;
        }

        BSTR bstr = SysAllocString(L"XMLFile.xml");
        VARIANT_BOOL varStatus;
        pIXMLDOMDocument->loadXML(bstr, &varStatus);
        if (varStatus == VARIANT_FALSE)
        {
        cout << "loadXML - XMLFile.xml Failed" << endl;
        return -1;
        }

        IXMLDOMNodeList* nodeList;
        IXMLDOMNode* node;
        BSTR bstr1 = SysAllocString(L"stu_rec");
        VARIANT var;
        long len;

        pIXMLDOMDocument->getElementsByTagName(bstr1, (&nodeList));
        if (nodeList->get_length(&len) > 0)
        {
        nodeList->get_item(1, &node);
        node->get_nodeValue(&var);
        cout << "var = " << var.bstrVal << endl;
        }

        cout << "main - end" << endl;
        ::CoUninitialize();

        return 0;
    }


Popular posts from this blog

OBJECT ORIENTED ANALYSIS AND DESIGN (OOAD)

OBJECT ORIENTED PROGRAMMING

STARTING A BUSINESS