Read XML in VC ++

Source: Internet
Author: User
There are several ways to read XML in VC ++. The visual tinyxml is the simplest. For your convenience, let's briefly describe the process. If you have any questions, please leave a message: 1. Download tinyxmltinyxml, you can find and decompress the file in the file, which consists of tinyxml and a static link library file xmladapter. lib2 and tinyxml usually need to be included in your own project file, but you can also choose the following method: a. Choose project> Settings> tab link> category and select input> objects/library modules to add xmladapter. LIB; add xmladapter path B under addtional library path. The project contains the following header file # include "tinyxml/tinystr. h"
# Include "tinyxml/tinyxml. H "3. You can start using tinyxml to operate XML files. The specific operation method is tinyxml, which is an open-source parsing XML parsing library and can be used in C ++, can be compiled in Windows or Linux. The parsing library's model parses the XML file and then generates the DOM model in the memory, so that we can easily traverse the XML tree of this course.
Note: The Dom model refers to the Document Object Model, which divides the entire document into multiple elements (such as books, chapters, sections, and segments ), the tree structure is used to represent the ordered relationship between these elements and the nested inclusion relationship (readers who understand the HTML language can easily understand this tree model ).
The following is an XML snippet:
<Persons>
<Person id = "1">
<Name> Zhou Xingxing </Name>
<Age> 20 </age>
</Person>
<Person id = "2">
<Name> Bai Jingjing </Name>
<Age> 18 </age>
</Person>
</Persons>
In tinyxml, some classes are defined based on various elements of XML:
Tixmlbase: The base class of the entire tinyxml model.
Tixmlattribute: attribute corresponding to the element in XML.
Tixmlnode: corresponds to a node in the DOM structure.
Tixmlcomment: corresponds to the comment in XML.
Tixmldeclaration: corresponds to the declarative part in XML, that is, <? Versiong = "1.0"?>.
Tixmldocument: corresponds to the entire XML document.
Tixmlelement: Elements corresponding to XML.
Tixmltext: corresponds to the text section of XML.
Tixmlunknown: corresponds to the unknown part of XML.
Tixmlhandler: defines some XML operations.
How can we use these classes and their methods to manipulate our XML? See the following.
1. Read XML (assume that the content in our XML document is the same as that in the preceding XML document)
// Create an XML document object.
Tixmldocument * mydocument = new tixmldocument ("fill in your xml file name ");
Mydocument-> LoadFile ();
// Obtain the root element, namely, persons.
Tixmlelement * rootelement = mydocument. rootelement ();
// Output the root element name, namely, the output persons.
Cout <rootelement-> value () <Endl;
// Obtain the first person node.
Tixmlelement * firstperson = rootelement-> firstchildelement ();
// Obtain the name and age nodes and ID attributes of the first person.
Tixmlelement * nameelement = firstperson-> firstchildelement ();
Tixmlelement * ageelement = nameelement-> nextsiblingelement ();
Tixmlattribute * idattriperson = firstperson-> firstattribute ();
// Output the name of the first person, that is, Zhou Xing; age, that is, 20; Id attribute, that is, 1.
Cout <nameelement-> firstchild ()-> value <Endl;
Cout <ageelement-> firstchild ()-> value <Endl;
Cout <idattribute-> value () <Endl;


Check whether it is very easy to read XML. It is very similar to the XML parsing library of Java, that is, the name has been changed.
Ii. Generate XML content
// Create an XML document object.
Tixmldocument * mydocument = new tixmldocument ();
// Create a root element and connect it.
Tixmlelement * rootelement = new tixmlelement ("persons ");
Mydocument-> linkendchild (rootelement );
// Create a person element and connect it.
Tixmlelement * personelement = new tixmlelement ("person ");
Rootelement-> linkendchild (personelement );
// Set the attributes of the person element.
Personelement-> setattribute ("ID", "1 ");
// Create and connect the name and age elements.
Tixmlelement * nameelement = new tixmlelement ("name ");
Tixmlelement * ageelement = new tixmlelement ("Age ");
Personelement-> linkendchild (nameelement );
Personelement-> linkendchild (ageelement );
// Set and connect the content of the name and age elements.
Tixmltext * namecontent = new tixmltext (" ");
Tixmltext * agecontent = new tixmltext ("20 ");
Nameelement-> linkendchild (namecontent );
Ageelement-> linkendchild (agecontent );
// Save to file
Mydocument-> SaveFile ("XML file name to be saved ");
In this way, the following XML file is created:
<Persons>
<Person id = "1">
<Name> Zhou Xingxing </Name>
<Age> 20 </age>
</Person>
</Persons>

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.