Use tinyXML in C ++ and tinyxml

Source: Internet
Author: User

Use tinyXML in C ++ and tinyxml
TinyXML is a very good C ++ class library for operations. The file is not large, but the method is rich. It can be invincible with apache Dom4j! I am used to using the java class library. I am very happy to see such a rich c ++ class library! It is easy to use. You only need to copy a few files to your project and compile and run it without STL.

The following describes how to use and understand the tinyXML class library from these aspects.

First download the tinyXML class library on sourceforge, address: http://sourceforge.net/projects/tinyxml/

Decompress tinyXML and add these six files to your c ++ project.Tinystr. h, tinystr. cpp, tinyxml. h, tinyxml. cpp, tinyxmlerror. cpp, tinyxmlparser. cpp. Use the following code to introduce the tinyXML class library where you need to operate the xml file. C ++ code

  1. # Include <tinyxml>

Or C ++ code
  1. # Include "tinyxml. h"


Below is a simple example to illustrate how to use tinyXML to operate xml files. Before giving an example, let me talk about the correspondence between the main classes in tinyXML and xml documents. The following is a class diagram of the main classes in tinyXML, reflecting the static relationship between classes.

Reference from tinyXML

TiXmlBase is the base class of all classes. Both TiXmlNode and TiXmlAttribute inherit from the TiXmlBase class. TiXmlNode class refers to all... <... /> the content, and the nodes in xml are divided into the following aspects: Declaration, comment, node, and text between nodes, therefore, based on TiXmlNode, these classes of TiXmlComment, TiXmlDeclaration, TiXmlDocument, TiXmlElement, TiXmlText, and TiXmlUnknown are derived to indicate which part of xml is specific. TiXmlAttribute is different from TiXmlNode. It refers to the content in angle brackets, such as <... *** =...>. ***Is an attribute. Here is a detailed description in xml. The content is as follows:
Xml Code
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Phonebook>
  3. <! -- One item behalfs one contacted person. -->
  4. <Item>
  5. <Name> miaomaio </name>
  6. <Addr> Shaanxi Xi'an </addr>
  7. <Tel> 13759911917 </tel>
  8. <Email> miaomiao@home.com </email>
  9. </Item>
  10. <Item>
  11. <Name> gougou </name>
  12. <Addr> Liaoning Shenyang </addr>
  13. <Tel> 15840330481 </tel>
  14. <Email> gougou@home.com </email>
  15. </Item>
  16. <! -- More contacted persons. -->
  17. </Phonebook>

  • TiXmlDeclaration means <? Xml version = "1.0" encoding = "UTF-8"?>,
  • TiXmlComment indicates <! -- One item behalfs one contacted person. -->, <! -- More contacted persons. -->,
  • TiXmlDocument indicates the entire xml document,
  • For example, TiXmlElement refers to <phonebook>, <item>, <name>, <addr>, and so on,
  • TiXmlText refers to 'gougou' and '000000' clips, including <item> and </item>, <name> and </name>, <addr>, and </addr>. text,
  • TiXmlAttribute indicates <? Xml version = "1.0" encoding = "UTF-8"?> Version, encoding,
  • In addition, TiXmlUnknown is used.


Below is a piece of c ++ code I wrote to read the xml file, and then write the source code of an item to the xml file. The content in phonebookdata. xml is the above xml, which is for reference only.
C ++ code
  1. //______________________________________________________________________
  2. // Read information from xml file.
  3. // Define xml file path, as follow, we use relative path,
  4. // But you can use absolute path also.
  5. Const char * filepath = "phonebookdata. xml ";
  6. TiXmlDocument doc (filepath );
  7. Bool loadOkay = doc. LoadFile ();
  8. // Faile to load 'phonebookdata. xml '.
  9. If (! LoadOkay ){
  10. Printf ("cocould not load test file % s. Error = '% s'. Exiting. \ n", filepath, doc. ErrorDesc ());
  11. Exit (1 );
  12. }
  13. // Get dom root of 'phonebookdata. xml', here root shoshould be 'phonebook '.
  14. TiXmlElement * root = doc. RootElement ();
  15. Printf ("_______________________________________ \ n ");
  16. Printf ("contacted person information \ n ");
  17. // Trace every items below root.
  18. For (TiXmlNode * item = root-> FirstChild ("item ");
  19. Item;
  20. Item = item-> NextSibling ("item ")){
  21. Printf ("_______________________________________ \ n ");
  22. // Read name.
  23. TiXmlNode * child = item-> FirstChild ();
  24. Const char * name = child-> ToElement ()-> GetText ();
  25. If (name ){
  26. Printf ("name: % s \ n", name );
  27. } Else {
  28. Printf ("name: \ n ");
  29. }
  30. // Read address.
  31. Child = item-> IterateChildren (child );
  32. Const char * addr = child-> ToElement ()-> GetText ();
  33. If (addr ){
  34. Printf ("addr: % s \ n", addr );
  35. } Else {
  36. Printf ("addr: \ n ");
  37. }
  38. // Read telephone no.
  39. Child = item-> IterateChildren (child );
  40. Const char * tel = child-> ToElement ()-> GetText ();
  41. If (tel ){
  42. Printf ("tel: % s \ n", tel );
  43. } Else {
  44. Printf ("tel: \ n ");
  45. }
  46. // Read e-mail.
  47. Child = item-> IterateChildren (child );
  48. Const char * email = child-> ToElement ()-> GetText ();
  49. If (email ){
  50. Printf ("email: % s \ n", email );
  51. } Else {
  52. Printf ("email: \ n ");
  53. }
  54. Printf ("\ n ");
  55. }
  56. //______________________________________________________________________
  57. //______________________________________________________________________
  58. // Add information to xml file and save it.
  59. TiXmlElement * writeRoot = doc. RootElement ();
  60. TiXmlNode * newNode = new TiXmlElement ("item ");
  61. Const TiXmlNode * name4NewNode = new TiXmlElement ("name ");
  62. NewNode-> InsertEndChild (* name4NewNode)-> InsertEndChild (TiXmlText ("pipi "));
  63. Const TiXmlNode * addr4NewNode = new TiXmlElement ("addr ");
  64. NewNode-> InsertEndChild (* addr4NewNode)-> InsertEndChild (TiXmlText ("Shaanxi Xianyang "));
  65. Const TiXmlNode * tel4NewNode = new TiXmlElement ("tel ");
  66. NewNode> InsertEndChild (* tel4NewNode)-> InsertEndChild (TiXmlText ("02937310627 "));
  67. Const TiXmlNode * email4NewNode = new TiXmlElement ("email ");
  68. NewNode-> InsertEndChild (* email4NewNode)-> InsertEndChild (TiXmlText ("pipi@home.com "));
  69. WriteRoot-> InsertEndChild (* newNode );
  70. Doc. SaveFile ();
  71. //______________________________________________________________________


For more information, see the document with tinyxml.
Can I use tinyxml to read and write xml in C language? Urgent

Tinyxml is encapsulated into a class, and there is no class in C language, so there is a problem at the syntax level.
However, you can wrap tinyxml in a dll to provide functions for C language calls.

How does tinyxml use the MFC program or whether there is an example program sent to me 1285280276 @ qqcom?

Not add. h and. cpp to the project, and then reference the header file

// Read the configuration file
CString str, strFileName = _ T ("c: \ config. xml ");
TiXmlDocument docXML (strFileName );
If (docXML. LoadFile ())
{
// File handle
TiXmlHandle docHandle (& docXML );

// Node Element
TiXmlElement * pStartUp = docHandle. FirstChildElement (_ T ("first-level node name"). FirstChildElement (_ T ("second-level node name"). Element ();
Str = pStartUp-> Attribute (_ T ("secondary node Attribute "));
}

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.