您的位置:首页 > 编程语言 > C语言/C++

pugixml 学习笔记一 加载文件

2016-05-10 13:36 309 查看
// pugixmltest.cpp : Defines the entry point for the console application.  

//  

  

#include "stdafx.h"  

//可以是相对路径,或者绝对路径  

bool LoadFromFile(const char * szXmlFileName)  

{  

    pugi::xml_document doc;  

      

    pugi::xml_parse_result result = doc.load_file(szXmlFileName);  

      

    std::cout << "Load result: " << result.description() <<std::endl  

        << "name.attrib = " << doc.child("Name").attribute("age").value() << std::endl;  

  

    return true;  

}  

  

bool LoadFromBuffer()  

{  

      

    const char *szBuffer="<?xml version=\"1.0\"?><Name age=\"10\">good</Name>";  

    pugi::xml_document doc;  

    if (!doc.load_buffer(szBuffer,strlen(szBuffer)))   

        return false;                                                         

  

    std::cout<< "name.attrib = " << doc.child("Name").attribute("age").value() << std::endl;  

    return true;  

      

}  

bool LoadFromMem()  

{  

    const char *szBuffer="<?xml version=\"1.0\"?><Name age=\"10\">good</Name>";  

    pugi::xml_document doc;  

    if (!doc.load(szBuffer))   

        return false;                                                         

      

    std::cout<< "name.attrib = " << doc.child("Name").attribute("age").value() << std::endl;  

    return true;  

}  

bool LoadFromStream()  

{  

    std::ifstream stream("tree.xml");  

    pugi::xml_document doc;  

    if (!doc.load(stream))  

    {  

        return false;  

    }  

     std::cout<< "name.attrib = " << doc.child("Name").attribute("age").value() << std::endl;  

     return true;  

}  

  

int main(int argc, char* argv[])  

{  

    //LoadFromFile("tree.xml");  

    //LoadFromBuffer();  

    //LoadFromMem();  

    LoadFromStream();  

    return 0;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pugixml C++