zoukankan      html  css  js  c++  java
  • 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
    知识共享,欢迎转载。
  • 相关阅读:
    缓存 memcached 与 redis
    爬了个爬(三)Scrapy框架
    算法 ----- 希尔排序
    算法 ----- 计数排序
    hibernate多对一单向外键
    HIBERNATE一对一双向外键联合主键关联
    hibernate一对一双向外键关联
    Hibernate一对一单向外键关联
    Hibernate关系级别注解
    Java在mysql插入数据的时候的乱码问题解决
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834859.html
Copyright © 2011-2022 走看看