5、节点内容

    pugi::xml_document doc;
    doc.load_string("<project><name>test</name><version>1.1</version><public>yes</public></project>"); //载入内容
    doc.print(std::cout); //展示内容

    pugi::xml_node project = doc.child("project");//节点project的句柄

    // 子节点的内容
    std::cout << "Project name: " << project.child("name").text().get() << std::endl; //直接获取
    std::cout << "Project version: " << project.child("version").text().as_double() << std::endl; //转double获取
    std::cout << "Project visibility: " << (project.child("public").text().as_bool(/* def= */ true) ? "public" : "private") << std::endl; //依据bool值输出不同内容
    std::cout << "Project description: " << project.child("description").text().get() << std::endl; //无此节点,不会报错

    // 修改节点version的内容
    project.child("version").text() = 1.2;

    // 增加节点description(project的子节点),设置内容
    project.append_child("description").text().set("a test project");

    doc.print(std::cout); //打印内容
    doc.save_file("2.xml"); //保存到文件

 

posted @ 2019-12-31 17:06  夕西行  阅读(365)  评论(0编辑  收藏  举报