The C ++ programming language can help us easily implement many functions. For example, operations on files, and the C ++ TinyXml operation methods used in C ++ parsing XML that we will introduce to you today. I hope that beginners can fully master the application skills in this area by interpreting the content introduced in this article.
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 select STL, you can use std: string)
- TINYXML_USE_STL: = YES
I. Features of C ++ TinyXml
- Detailed description of the C ++ explicit keyword Application Method
- C ++ function Overloading is implemented through C Language
- Questions about C ++ polymorphism coverage
- Overview of C ++ streaming File Operations
- C ++ strtok () Implementation Function Analysis
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. At the same time, 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 C ++ 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. State Library with STL and without STL), it 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 the corresponding 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: by default, STL is not supported for compilation. You can edit TINYXML_USE_STL: = NO in 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. Optional VC), you can use the/DTIXML_USE_STL parameter. If it is too troublesome, you can directly define it in the tinyxml. h file.
III. C ++ 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 C ++ TinyXml document.
2. Notes
Conversions between various types
Since each node class inherits from TiXmlNode, you often need to convert the TiXmlNode * type pointer to the pointer of its derived class during use. 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 C ++ 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.