Introduction

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

Copyright

Downloading, installing or using XmlParser (the software) signifies acceptance of these terms and conditions of the license.

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:

  1. Include XmlParser.h.

  2. Declare a CXMLParse object and three null DOMNode pointers.

  3. 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.

  4. Specify the XML file name and start parsing. The root node of the whole XML hierarchy will be assigned to "root_node"

  5. Find the node that is rooted at "contact_list", get the attribute "last_update" and print it out.

  6. Find the first node of "contact" and assign it to "contact_node". Iterate until all "contact" nodes have been visited.

  7. 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.

  8. 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.

  9. This is the complete list of code.

  10. This is the output of the program.


XmlParser © 2006 Fengjun Lv
Last update: 10/10/2006 (check html)