• tinyxml


    <?xml version="1.0" ?>  
    <MyApp>  
        <Messages>  
            <Welcome>Welcome to MyApp</Welcome>  
            <Farewell>Thank you for using MyApp</Farewell>  
        </Messages>  
        <Windows>  
            <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
        </Windows>  
        <Connection ip="192.168.0.1" timeout="123.456000" />  
    </MyApp>  

    制作一个快递查询的软件,需要处理XML数据,系统的学习下XML

    #include <iostream>
    #include "tinyxml.h"
    #pragma comment(lib, "tinyxml.lib")
    using namespace std;
    
    void CreateXml(string XmlFile)
    {
    	TiXmlDocument *doc = new TiXmlDocument;
    
    	TiXmlDeclaration *dec = new TiXmlDeclaration("1.0", "", "");
    	doc->LinkEndChild(dec);
    
    	TiXmlElement *root = new TiXmlElement("MyApp");
    	doc->LinkEndChild(root);
    
    	TiXmlElement *mess = new TiXmlElement("Messages");
    	root->LinkEndChild(mess);
    
    	TiXmlElement *welc = new TiXmlElement("Welcome");
    	mess->LinkEndChild(welc);
    
    	TiXmlText *welctext = new TiXmlText("Welcome to MyApp");
    	welc->LinkEndChild(welctext);
    
    	TiXmlElement *fare = new TiXmlElement("Farewell");
    	mess->LinkEndChild(fare);
    
    	TiXmlText *faretext = new TiXmlText("Thank you for using MyApp");
    	fare->LinkEndChild(faretext);
    
    	TiXmlElement *wind = new TiXmlElement("Windows");
    	root->LinkEndChild(wind);
    
    	TiXmlElement *win = new TiXmlElement("Window");
    	wind->LinkEndChild(win);
    
    	win->SetAttribute("name", "MainFrame");
    	win->SetAttribute("x", "5");
    	win->SetAttribute("y", "15");
    	win->SetAttribute("w", "400");
    	win->SetAttribute("h", "250");
    
    	TiXmlElement *conn = new TiXmlElement("Connection");
    	root->LinkEndChild(conn);
    
    	conn->SetAttribute("ip", "192.168.0.1");
    	conn->SetAttribute("timeout", "123.456000");
    
    	doc->SaveFile(XmlFile.c_str());
    }
    
    void ReadXml(string XmlFile)
    {
    	TiXmlDocument *doc = new TiXmlDocument;
    	doc->LoadFile(XmlFile.c_str());
    	doc->Print();
    }
    
    int main(void)
    {
    	string XmlFile("text.xml");
    	CreateXml(XmlFile);
    	ReadXml(XmlFile);
    	return 0;
    }


    使用STL版本则需要

    #define TIXML_USE_STL
    #pragma comment(lib, "tinyxmlSTL.lib")



    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    10个对Web开发者最有用的Python包
    9款最好的JavaScript压缩工具
    推荐15款制作 SVG 动画的 JavaScript 库
    2016年最好的15个Web设计和开发工具
    整理六百篇web前端知识混总
    9款免费的跨浏览器测试工具
    9个有用的和免费的工具来支持动态网页开发
    8个基本的引导工具的网页设计师
    11款CSS3动画工具的开发
    2016年某前端群题目答案参考
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834859.html
Copyright © 2020-2023  润新知