TinyXml如何序列化

发布于 2024-11-07 16:02:20 字数 217 浏览 0 评论 0原文

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

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

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

EDIT:
How can I serialize an xml using tinyxml?

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

Question 2: Can I deserialize the xml i've created with the same library tinyxml? does it have this advantage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-11-14 16:02:20

您是否尝试过阅读文档?第一页上有打印功能的详细信息。这些文档链接到教程,其中包含写入和读取文件的示例:

#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: %s\n", 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: %s\n", settings.m_name.c_str(), settings.m_messages["Farewell"].c_str());
    }
    return 0;
}

Have you tried reading the documentation? There's details of the print function on that first page. Those docs link to a tutorial which has an example of writing to and reading from a file:

#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: %s\n", 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: %s\n", settings.m_name.c_str(), settings.m_messages["Farewell"].c_str());
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文