XML Parser






1.31/5 (4 votes)
Jul 21, 2006
1 min read

31648

782
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 :)).