Explanation of C ++ implementation parsing XML TinyXML

Source: Internet
Author: User

Modify the makefile file in front of the mingw32-make as follows:
# DEBUG can be set to YES to include debugging info, or NO otherwise (not DEBUG)
DEBUG: = NO

# PROFILE can be set to YES to include profiling info, or NO otherwise
PROFILE: = NO

# TINYXML_USE_STL can be used to turn on STL support. NO, then STL
# Will not be used. YES will include the STL files. (If you use STL, you can use std: string)
TINYXML_USE_STL: = YES

I. Features of TinyXml
TinyXml is a lightweight C interpreter based on the DOM model.

1. SAX and DOM
Currently, XML parsing mainly involves two models: SAX and DOM.
Among them, SAX is event-based, and its basic workflow is to analyze XML documents. When a new element is found, a corresponding event is generated and corresponding user processing functions are called. In this way, the memory usage is small and the speed is fast, but the user program is complicated accordingly.
The DOM (Document Object Model) analyzes the entire XML document at one time during analysis, and forms the corresponding tree structure in the memory, provides you with a series of interfaces to access and edit the tree structure. This method occupies a large amount of memory and is often slower than SAX. However, it can provide users with an object-oriented access interface, which is more user-friendly.

2. Verification and non-verification
For a specific XML document, its correctness is divided into two levels. First, the format must comply with the basic XML format requirements. For example, the first line must have a declaration, and the label nesting layers must be consistent, is a qualified XML file called well-formatted. However, an XML document must meet the corresponding standards in terms of semantics because of its different content. These standards are defined by the corresponding DTD file or Schema file, XML files that meet these definition requirements are called valid.
Therefore, the parser can be divided into two types. One is for verification, that is, it will verify the XML file with the corresponding DTD file according to the declaration in the XML file, check whether it meets the requirements of the DTD file. The other is to ignore the DTD file and parse it as long as the basic format is correct.
As far as I know, the validators are usually heavyweight. TinyXml does not support verification, but is small in size. It is particularly suitable for parsing XML files with simple formats, such as configuration files.

Ii. Build and use TinyXml
1. Get
On the TinyXml homepage at http://www.grinninglizard.com/tinyxml/index.html, you can find the latest source code. The current version is 2.4.3 (as of 2006.5.17 ).

2. Build
When building TinyXml, you can choose whether to support STL. If you select it, you can use std: string. Therefore, the VC6 project file is usually provided in the TinyXml source code package on Windows, you can use it to generate two static open options. The State Library (with STL and without STL) is very easy. Note that the default library is single-threaded. If it is used in a multi-threaded project, you need to modify the configuration to generate a multi-threaded library.
On Unix platforms, only one Makefile is provided in the TinyXml source code package. For a typical Linux system or other Unix systems installed with gcc and gmake, this Makefile is enough, I tested it on RH9 and RHEL4, and the simple make was successful. Note the following: STL is not supported by default. You can edit TINYXML_USE_STL: = NO in the Makefile and change NO to YES to support STL; by default, only one test program is generated without any libraries. To generate a static library, you can use the ar command to package the generated target files, to generate a dynamic library, you must add the-fpic parameter to recompile the library.

3. Use
After the corresponding libraries are built, you only need to connect them in the project that uses them. Note that, if STL is required, you need to define a Macro Program (VC) when compiling a TinyXml file. You can use the/DTIXML_USE_STL parameter, can be directly defined in tinyxml. h file.

Iii. TinyXml Programming Model

1. Relationships between classes
The DOM access model is implemented in TinyXml. Therefore, a series of classes are provided to correspond to each node in the XML file. Shows the relationships between main classes:
TiXmlBase: The base class of other classes. It is an abstract class.
TiXmlNode: indicates a node that contains common methods of nodes, such as accessing a self-node, sibling node, editing itself, and editing a subnode.
TiXmlDocument: indicates the entire XML document, which does not correspond to a specific node.
TiXmlElement: indicates an element node. It can contain subnodes and TiXmlAttribute.
TiXmlComment: Comment
TiXmlDeclaration: indicates the Declaration
TiXmlText: indicates a text node.
TiXmlUnknown: Unknown node, usually Error
TiXmlAttribute: indicates the attribute of an element.
The following is a simple example:
<? Xml version = "1.0" encoding = "UTF-8"?>
<! -This is only a sample -->
<Book>
<Name> TinyXml How To </name>
<Price unit = "RMB"> 20 </price>
<Description> Some words... </Description>
</Book>
The entire document corresponds to TiXmlDocument
Book, name, price, description, all correspond to TiXmlElement
The first line corresponds to a TiXmlDeclaration
The second row corresponds to a TiXmlComment.
"TinyXml How To" corresponds To a TiXmlText
Unit is a TiXmlAttribute of price.
These classes have a good relationship with the corresponding elements in the XML file. Therefore, we believe that you can easily master the use of each method by referring to the TinyXml document.

2. Notes
Conversions between various types
Each node class inherits from TiXmlNode. When using TiXmlNode *, you often need to convert the pointer of TiXmlNode * type to the pointer of its derived class. During this conversion, we should first use a series of conversion functions provided by the TiXmlNode class, such as ToElement (void), instead of dynamic_cast of c.
Check return values
Because TinyXml is a non-validation parser, when parsing a file, it is likely that the file does not contain a certain node we expected. In this case, TinyXml will return a null pointer. Therefore, you must check the returned values. Otherwise, memory access errors may easily occur.
How to recreate an XML file
Create a TiXmlDocument object first, load a template, or directly insert a node as the root node, and then operate on it as you open an existing XML file.

Iv. Summary
The biggest feature of TinyXml is that it is very small and can be easily statically connected to the program. It is suitable for parsing files such as configuration files and simple data files. However, because it is not verified, it requires a lot of checks in the program, increasing the burden of programming. Therefore, for complex XML files, I think it is best to use a verified Parser for processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.