C ++ generates XML (using tinyxml)

Source: Internet
Author: User

The parsing and library installation methods are in the previous article.

I write a routine according to the XML style on this website.

Due to the large number of original XML attributes, I only took three tags and each tag took four attributes to simplify the process.

The Code is as follows:

#include <iostream>#include "tinyxml.h"using namespace std;#pragma comment(lib,"tinyxml.lib")const char* AttributeName[4]={"TITLE","ARTIST","PRICE","YEAR"};const char* Attribute[4][3]={{"Empire Burlesque","Hide your heart","Greatest Hits"},                             {"Bob Dylan","Bonnie Tyler","Dolly Parton"},                             {"10.90","9.90","9.90"},                             {"1985","1988","1982"}};int main(){    const char* xmlFile = "lianxi.xml";    TiXmlDocument doc;          TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "", "");     doc.LinkEndChild(decl);    TiXmlElement* firstLevel=new TiXmlElement("CSTALOG");    firstLevel->SetAttribute("CD","3");    firstLevel->SetAttribute("Attribute","4");        for (int i=0;i<3;i++)    {        TiXmlElement* secondLevel=new TiXmlElement("CD");        for (int j=0;j<4;j++)        {            TiXmlElement* thirdLevel=new TiXmlElement(AttributeName[j]);            thirdLevel->LinkEndChild(new TiXmlText(Attribute[j][i]));            secondLevel->LinkEndChild(thirdLevel);        }        firstLevel->LinkEndChild(secondLevel);    }                doc.LinkEndChild(firstLevel);    doc.SaveFile(xmlFile);     return 0;}

It seems that there is a memory leak, but for a clear display structure, no matter how much.

Finally, the following file is generated:

<?xml version="1.0" ?><CSTALOG CD="3" Attribute="4">    <CD>        <TITLE>Empire Burlesque</TITLE>        <ARTIST>Bob Dylan</ARTIST>        <PRICE>10.90</PRICE>        <YEAR>1985</YEAR>    </CD>    <CD>        <TITLE>Hide your heart</TITLE>        <ARTIST>Bonnie Tyler</ARTIST>        <PRICE>9.90</PRICE>        <YEAR>1988</YEAR>    </CD>    <CD>        <TITLE>Greatest Hits</TITLE>        <ARTIST>Dolly Parton</ARTIST>        <PRICE>9.90</PRICE>        <YEAR>1982</YEAR>    </CD></CSTALOG>

 

 

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.