XmlParser is a lightweight open source C++ library based on Apache's Xerces C++ Parser
Post date: May 4, 2011 4:05:09 AM
eIntroduction
XmlParser is a lightweight open source C++ library based on Apache's Xerces C++ Parser. XmlParser contains only two files (one .cpp and one .h) and provides simple API for parsing XML files. XmlParser supports charsets such as GB2312.
Downloads and Installation
Download and install Apache's Xerces C++ Parser from here. The latest version of Xerces that has been tested is 2.1.0 (a bit outdated, but it works very well).
Add the include and lib folder to your Visual C++ path list. By default they are "C:\Program Files\xerces-c2_1_0-win32\include" and "C:\Program Files\xerces-c2_1_0-win32\lib".
Add "xerces-c_2.lib" (or "xerces-c_2D.lib" for debug mode) to the library modules of your Visual C++ project, shown as follows.
Download and unzip XmlParser.
Copy and add XmlParser.cpp and XmlParser.h to your Visual C++ project.
XmlParser has been developed in Visual C++ 6.0 (with SP 5) on Windows OS. It has not been tested on other OS but it should also work (may need minor changes) because it is written in standard C++.
Copyright
Downloading, installing or using XmlParser (the software) signifies acceptance of these terms and conditions of the license.
XmlParser is Copyright © 2006 by Fengjun Lv. All rights reserved.
XmlParser is provided as Freeware. If you find XmlParser useful, the author of XmlParser would appreciate it if you mention the software in your publications or add the following banner on your site.
XmlParser is provided "AS-IS". No warranty of any kind is expressed or implied. The author will not be liable for data loss, damages, loss of profits or any other kind of loss while using or misusing the software.
Any trademarks mentioned here are the property of their respective owners.
A lightweight Tutorial
The concept of XML and DTD is beyond the scope of this tutorial. A good introduction to XML, DTD and XSLT can be found at http://www.w3schools.com/.
This zip file contains the Visual C++ project and sample data of this tutorial.
Suppose we have the following DTD and XML file for a contact list.
The DTD file
The XML file
We want to write C++ code to print out this contact list in plain text. We will do the following steps:
Include XmlParser.h.
Declare a CXMLParse object and three null DOMNode pointers.
Initialize the parser. By default, the parser will check the validity of the XML file based on its DTD file. Anything that is not valid will be displayed. To disable the validity check, use "parser.Init(false)" instead.
Specify the XML file name and start parsing. The root node of the whole XML hierarchy will be assigned to "root_node"
Find the node that is rooted at "contact_list", get the attribute "last_update" and print it out.
Find the first node of "contact" and assign it to "contact_node". Iterate until all "contact" nodes have been visited.
For the current "contact" node, get attributes such as name, email, company, cell phone and print them out. Note that because the attribute of company and cell phone is optional, we need to determine if the returned value is empty or not.
If the current "contact" node has one or multiple "address" child nodes, get the first node and assign it to the "address_node". Get attributes and print them out. Iterate until all "address" nodes have been visited.
This is the complete list of code.
This is the output of the program.
-Courtesy http://vipbase.net/xmlparser/