c++ xml支持

<xml>

*    方法和json类似,从 sourceforge.net/projects/tinyxml 下载源文件解压
*    在vs工程属性中 configuration properties -> C/C++ -> general -> additional include directories 添加 tinyxml.h的路径
*    然后再工程目录下新建一个filter,命名xml,添加解压出来的文件的 .cpp / .h 六个文件(除了xxxtest.cpp,因为是测试文件,里面有main函数),
*    其中.cpp 文件的预编译头属性precompiled header设置成no,
*    在主函数中加入头文件引用即可

#include "tinystr.h"   
#include "tinyxml.h"
    //读取在工程目录中的文件demotest.xml
    TiXmlDocument doc("demotest.xml");
    doc.Parse(demoStart);

    if (doc.Error())//读取失败
    {
        printf("Error in %s: %s\n", doc.Value(), doc.ErrorDesc());
        exit(1);
    }
    doc.SaveFile();

    bool loadOkay = doc.LoadFile();

    if (!loadOkay)
    {
        printf("Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc());
        exit(1);
    }

    //两种输出方式,结果看起来都差不多,整体打印出来
    printf("** Demo doc read from disk: ** \n\n");
    printf("** Printing via doc.Print **\n");
    doc.Print(stdout);


    printf("\n\n\n** Printing via TiXmlPrinter **\n\n");
    TiXmlPrinter printer;
    doc.Accept(&printer);
    fprintf(stdout, "%s", printer.CStr());

//根据节点修改属性
    // --------------------------------------------------------
    // An example of changing existing attributes, and removing
    // an element from the document.
    // --------------------------------------------------------
    TiXmlNode* node = 0;
    TiXmlElement* todoElement = 0;
    TiXmlElement* itemElement = 0;

    // Get the "ToDo" element.
    // It is a child of the document, and can be selected by name.
    node = doc.FirstChild( "ToDo" );
    assert( node );
    todoElement = node->ToElement();
    assert( todoElement  );

    // Going to the toy store is now our second priority...
    // So set the "priority" attribute of the first item in the list.
    node = todoElement->FirstChildElement();    // This skips the "PDA" comment.
    assert( node );
    itemElement = node->ToElement();
    assert( itemElement  );
    itemElement->SetAttribute( "priority", 2 );

    // Change the distance to "doing bills" from
    // "none" to "here". It's the next sibling element.
    itemElement = itemElement->NextSiblingElement();
    assert( itemElement );
    itemElement->SetAttribute( "distance", "here" );

    // Remove the "Look for Evil Dinosaurs!" item.
    // It is 1 more sibling away. We ask the parent to remove
    // a particular child.
    itemElement = itemElement->NextSiblingElement();
    todoElement->RemoveChild( itemElement );

    itemElement = 0;

    // --------------------------------------------------------
    // What follows is an example of created elements and text
    // nodes and adding them to the document.
    // --------------------------------------------------------

//添加属性及字段
    // Add some meetings.
    TiXmlElement item( "Item" );
    item.SetAttribute( "priority", "1" );
    item.SetAttribute( "distance", "far" );

    TiXmlText text( "Talk to:" );

    TiXmlElement meeting1( "Meeting" );
    meeting1.SetAttribute( "where", "School" );

    TiXmlElement meeting2( "Meeting" );
    meeting2.SetAttribute( "where", "Lunch" );

    TiXmlElement attendee1( "Attendee" );
    attendee1.SetAttribute( "name", "Marple" );
    attendee1.SetAttribute( "position", "teacher" );

    TiXmlElement attendee2( "Attendee" );
    attendee2.SetAttribute( "name", "Voel" );
    attendee2.SetAttribute( "position", "counselor" );

//指定层级结构
    // Assemble the nodes we've created:
    meeting1.InsertEndChild( attendee1 );
    meeting1.InsertEndChild( attendee2 );

    item.InsertEndChild( text );
    item.InsertEndChild( meeting1 );
    item.InsertEndChild( meeting2 );

    // And add the node to the existing list after the first child.
    node = todoElement->FirstChild( "Item" );
    assert( node );
    itemElement = node->ToElement();
    assert( itemElement );

    todoElement->InsertAfterChild( itemElement, item );
//打印
    printf( "\n** Demo doc processed: ** \n\n" );
    doc.Print( stdout );

//前后对比
//------ origin
<?xml version="1.0" standalone="no" ?>
<!-- Our to do list data -->
<ToDo>
    <!-- Do I need a secure PDA? -->
    <Item priority="1" distance="close">
        Go to the
        <bold>Toy store!</bold>
    </Item>
    <Item priority="2" distance="none">Do bills</Item>
    <Item priority="2" distance="far &amp; back">Look for Evil Dinosaurs!</Item>
</ToDo>

//------ after
<?xml version="1.0" standalone="no" ?>
<!-- Our to do list data -->
<ToDo>
    <!-- Do I need a secure PDA? -->
    <Item priority="2" distance="close">Go to the
        <bold>Toy store!</bold>
    </Item>
    <Item priority="1" distance="far">Talk to:
        <Meeting where="School">
            <Attendee name="Marple" position="teacher" />
            <Attendee name="Voel" position="counselor" />
        </Meeting>
        <Meeting where="Lunch" />
    </Item>
    <Item priority="2" distance="here">Do bills</Item>
</ToDo>

避免 XML 属性?
因使用属性而引起的一些问题:

属性无法包含多重的值(元素可以)
属性无法描述树结构(元素可以)
属性不易扩展(为未来的变化)
属性难以阅读和维护
请尽量使用元素来描述数据。而仅仅使用属性来提供与数据无关的信息。

不要做这样的蠢事(这不是 XML 应该被使用的方式):

<note day="08" month="08" year="2008"
to="George" from="John" heading="Reminder" 
body="Don't forget the meeting!">
</note>

////////////////////////////////////
xml
https://www.cnblogs.com/xudong-bupt/p/3733306.html
**https://www.cnblogs.com/betterwgo/p/7895891.html

发布了64 篇原创文章 · 获赞 3 · 访问量 4575

猜你喜欢

转载自blog.csdn.net/qq_37631516/article/details/103787155