Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / XML
Article

XML Parser

Rate me:
Please Sign up or sign in to vote.
1.31/5 (4 votes)
21 Jul 20061 min read 30.8K   778   13   6
An article on an XML parser.

Introduction

Well, this is my first article on the Internet. This article will show how to read/write XML documents.

Background

It's a long time I have been working on networks. And I always had problems sending nested structures with multiple pointers over the network. Packing pointers and unpacking them was a real headache for me. Then came the idea of XML. Why not use XML to send data over the network? Although creating and reading XML docs is a little work on my part, life is much easy with it.

When I searched the Internet, I found xerces-c, a C library that could parse XML data. Well, it was not so simple using xerces-c (at least, I didn't find it easy). It had many complexities in it. So, I wrote this wrapper class using which I can generate XML data and also can read it back.

Using the code

I have attached a demo project showing how to read/write XML documents from/to files. Along with the code is a folder named "XML Files" which contains the xerces-c files supporting my wrapper.

The code is very easy to understand and use. It has got Set/Get functions through which we can easily generate XML documents. Here is the list of functions provided:

// Write the XML Document to a File
void  WriteToFile(char *filename);
// Generates an XML Document from an XML File
void   CreateDocumentFromFile(char *filename);
// Terminates XML Implementation
void   Terminate();
// Gets a float value from the tag Specified
float  GetFloatFromTag(char *);
// Gets a int value from the tag Specified
int    GetIntFromTag(char *tag);
// Creates an XML Document from a string
BOOL   CreateDocumentFromString(char *str);
// Destroys the Document not the Implementation 
void   DestroyDoc();
// Creates a new  XML Document from tag
BOOL   CreateDocument(char* data);
// Adds an element and add it to the Document
BOOL   CreateElement(char *node_tag,char* Node_Name, 
                     void *data, char DATA_TYPE,int len=0);
// Initializes Implantation
BOOL   Initialize();
// Gets a int array from the tag Specified
int*  GetIntegerArray(char *tag);
// Gets a float array from the tag Specified
float* GetFloatArray(char *tag);
// Gets a char string from the tag specified
char*  GetCharFromTag(char *tag);
// Converts the complete XML Document into a Char String
char*  GetXMLString();

These functions are self explanatory and are used well in the sample code. If you find any problems, you can always contact me.

Points of Interest

The most interesting part is the conversion of Unicode to ASCII . The most simplest way is to use ATL macros as used in the code. I know many people find it difficult to do so, but to me, it is the easiest thing to do :)).

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


Written By
Web Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHello Ahmad, I'd like to ask how to extract data within child nodes Pin
luckiejacky745696927-Dec-12 17:54
luckiejacky745696927-Dec-12 17:54 
AnswerRe: Hello Ahmad, I'd like to ask how to extract data within child nodes Pin
Ahmad Hassan27-Dec-12 23:08
Ahmad Hassan27-Dec-12 23:08 
QuestionWhat's wrong with TinyXml? Pin
Gilad Novik22-Jul-06 23:03
Gilad Novik22-Jul-06 23:03 
AnswerRe: What's wrong with TinyXml? Pin
Anonymuos22-Jul-06 23:55
Anonymuos22-Jul-06 23:55 
GeneralRe: What's wrong with TinyXml? Pin
Gilad Novik23-Jul-06 9:06
Gilad Novik23-Jul-06 9:06 
AnswerRe: What's wrong with TinyXml? Pin
Ahmad_Hassan23-Jul-06 4:45
Ahmad_Hassan23-Jul-06 4:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.