XML read and write

XML read and write

1.1 XML Document Format

  1.1.1 XML, EXtensible Markup Language Extensible Markup Language.

   In fact, the application of XML is generally relatively simple, not enough to a "language" level. XML is a data format, and the file suffix name that conforms to this format is usually *.xml
   Advantages: Strong readability!
   Disadvantages: time-consuming analysis, large space, low efficiency!

  1.1.2 XML Declaration

   The first line of an XML document must be a declaration statement: for example,

 <?xml version="1.0" encoding="GBK"?>  
 version 版本号:总是1.0
 encoding 文字编码:GBK, UTF-8等

  1.1.3 XML syntax

<?xml version="1.0" encoding="GBK"?>
 <root>  
<host> nihao.cn </host>
<port> 1230 </port>
</root> 

   In short, an XML document is a tree structure composed of elements. root, host, port are all elements

  1.1.3 Elements of XML

   Example:

<Contact  id="1001"  page="1">     
<Name> qixiaofan </Name>   
... 
</Contact> 
1. 名称Name:必须配对!
   <Name> shaofa </Name>   
<name> shaofa </name>   

   The following spelling is incorrect:

<NAME> qixiaofan </name> 

    NAME does not match name

Second, take engineering as an example

    Before that, let's introduce a very useful library, tinyxml.

   tinyxml: A tiny xml library that can generate and parse XML documents. Free and open source, you can directly add the source code to the project, suitable for a variety of platforms.
    It can be downloaded from the official website of the tinyxml author, portal .
    It can also be downloaded from CSDN, Portal .

  2.1 Writing XML

   The contents of the file written by the target:
write picture description here

Function implementation:

struct Note
{
    int id;
    char when[32];
    char who[32];
    char what[128];
};

vector<Note> notes;

int save()
{
    TiXmlDocument xml_doc;
    xml_doc.LinkEndChild(new TiXmlDeclaration( "1.0", "GBK", "" ));

    TiXmlElement * xml_root = new TiXmlElement("root");
    xml_doc.LinkEndChild(xml_root);

    TiXmlElement* xml_NoteList = new TiXmlElement("NoteList");
    xml_root->LinkEndChild(xml_NoteList);

    // 添加host元素
    for(int i=0; i<notes.size(); i++)
    {
        Note& r = notes[i];

        TiXmlElement* xml_Note = new TiXmlElement("Note");
        xml_NoteList->LinkEndChild(xml_Note);

        AfTinyXml::addChild(xml_Note, "id", r.id);
        AfTinyXml::addChild(xml_Note, "when", r.when);
        AfTinyXml::addChild(xml_Note, "who", r.who);
        AfTinyXml::addChild(xml_Note, "what", r.what);
    }

    //第一种方法:是将数据保存到本地
    xml_doc.SaveFile("example02b.xml");

    ////第二种方法:是将数据保存成string
    //string txt;
    //txt << xml_doc;
    //cout << txt << endl;

    return 0;
}

void addNote(int id, const char* when, const char* who, const char* what)
{
    Note n;
    n.id = id;
    strcpy(n.when, when);
    strcpy(n.who, who);
    strcpy(n.what, what);
    notes.push_back(n);
}

int main()
{
    addNote(1, "2018-1-26 1:00", "Qi", "睡觉");
    addNote(2, "2019-1-21 2:00", "Ge", "打豆豆");
    addNote(3, "2020-2-20 5:00", "Yong", "看书");

    save();
    return 0;
}

  2.1 XML reading.

   The read file is the same as the above write file.
   The function is implemented as follows:

struct Note
{
    int id;
    char when[32];
    char who[32];
    char what[128];
};

vector<Note> notes;

int main()
{
    // 解析xml
    TiXmlDocument xml_doc;
    if(!xml_doc.LoadFile("example02b.xml"))
    {
        return -1;
    }

    // 根节点
    TiXmlElement* xml_root = xml_doc.RootElement();
    if (NULL == xml_root)
    {
        return -1;
    }

    // 获取元素的文本与属性

    TiXmlElement* xml_NoteList = xml_root->FirstChildElement("NoteList");
    if(xml_NoteList)
    {
        TiXmlElement* xml_Note = xml_NoteList->FirstChildElement("Note");
        while(xml_Note)
        {
            // 取得子元素的文本
            int id = AfTinyXml::childAsInt(xml_Note, "id");
            string when = AfTinyXml::childAsText(xml_Note, "when");
            string who = AfTinyXml::childAsText(xml_Note, "who");           
            string what = AfTinyXml::childAsText(xml_Note, "what");

            // 保存到列表
            Note r;
            r.id = id;
            strcpy(r.when, when.c_str());
            strcpy(r.who, who.c_str());
            strcpy(r.what, what.c_str());
            notes.push_back(r);

            // 下一个兄弟元素
            xml_Note = xml_Note->NextSiblingElement("Note");
        }
    }


    return 0;
}

    So far, the simple usage of XML files has been summarized. For more complex read and write operations, further study of XML is required.
    Encapsulated project project files (including read and write operations, and PPT explanations of XML files): Portal .

  Personal public number:

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326442422&siteId=291194637