TinyXml 如何序列化



>编辑:如何使用 tinyxml 序列化 xml?

TiXmlDocument doc;
TiXmlElement * root;
root = new TiXmlElement( "Data" ); 
doc.SaveFile( "madeByHand.xml" );

问题 2:我可以反序列化我使用相同的库 tinyxml 创建的 xml 吗? 它有这个优势吗?

你试过阅读文档吗?第一页上有打印功能的详细信息。这些文档链接到一个教程,该教程具有写入和读取文件的示例:

#include <string>
#include <map>
using namespace std;
typedef std::map<std::string,std::string> MessageMap;
// a basic window abstraction - demo purposes only
class WindowSettings
{
public:
    int x,y,w,h;
    string name;
    WindowSettings()
        : x(0), y(0), w(100), h(100), name("Untitled")
    {
    }
    WindowSettings(int x, int y, int w, int h, const string& name)
    {
        this->x=x;
        this->y=y;
        this->w=w;
        this->h=h;
        this->name=name;
    }
};
class ConnectionSettings
{
public:
    string ip;
    double timeout;
};
class AppSettings
{
public:
    string m_name;
    MessageMap m_messages;
    list<WindowSettings> m_windows;
    ConnectionSettings m_connection;
    AppSettings() {}
    void save(const char* pFilename);
    void load(const char* pFilename);
    // just to show how to do it
    void setDemoValues()
    {
        m_name="MyApp";
        m_messages.clear();
        m_messages["Welcome"]="Welcome to "+m_name;
        m_messages["Farewell"]="Thank you for using "+m_name;
        m_windows.clear();
        m_windows.push_back(WindowSettings(15,15,400,250,"Main"));
        m_connection.ip="Unknown";
        m_connection.timeout=123.456;
    }
};
int main(void)
{
    // block: customise and save settings
    {
        AppSettings settings;
        settings.m_name="HitchHikerApp";
        settings.m_messages["Welcome"]="Don't Panic";
        settings.m_messages["Farewell"]="Thanks for all the fish";
        settings.m_windows.push_back(WindowSettings(15,25,300,250,"BookFrame"));
        settings.m_connection.ip="192.168.0.77";
        settings.m_connection.timeout=42.0;
        settings.save("appsettings2.xml");
    }
    // block: load settings
    {
        AppSettings settings;
        settings.load("appsettings2.xml");
        printf("%s: %sn", settings.m_name.c_str(), 
            settings.m_messages["Welcome"].c_str());
        WindowSettings & w=settings.m_windows.front();
        printf("%s: Show window '%s' at %d,%d (%d x %d)n", 
            settings.m_name.c_str(), w.name.c_str(), w.x, w.y, w.w, w.h);
        printf("%s: %sn", settings.m_name.c_str(), settings.m_messages["Farewell"].c_str());
    }
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新