当前位置: 首页 > 工具软件 > RapidXml > 使用案例 >

rapidxml使用手册3

姚晋
2023-12-01
整个解析器包含在一个文件中----rapidxml.hpp  不需要额外的编译连接,只需将其放在project中在程序中include进去。rapidxml_print.hpp包含了一些输出函数。

  rapidxml:

   xml_base 是 xml_node 和xml_attribute的基类 -----子类继承了:  Ch 类似char

          Ch* name() const;返回标签/属性的名字,如果没有便返回空串   “  ""   ”

          std::size_t name_size() const;----返回名字的字节数  不含结束符

          Ch* value() const; 返回标签/属性的值,如果没有返回空串

           std::size_t name_size() const


           xml_node<Ch>* parent() const;指向父节点的指针,如果没用则返回0 

          

  xml_base 中的方法:

#include <string>
#include <iostream>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_utils.hpp"
#include "rapidxml_print.hpp"


using namespace rapidxml;
using namespace std;

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

    //file<> fdoc("/mnt/win/tang.xml");//如果文件不存在,则报错

    //cout << fdoc.data() << endl;
    xml_document<> doc; //是解析器
    //doc.parse<0>(fdoc.data());//fdoc.data()取回的串格式和原来文件格式一样

    char a[] = "<top>"//如果单独传, 就不能加上xml的头部信息, 
                      //否则会报错
                    "<name>tangqiang</name>"
                    "<age>22</age>"
               "</top>";
    char* p = a;
    doc.parse<0>(p);

    xml_node<>* node = doc.first_node();//去顶级结点
    cout << (node->name())<< endl;
    node = node->first_node();
    while (node)
    {
        cout << node->name() << node->value() << endl;//name() value()返回的字符串不会去掉首尾的空白字符
        node = node->next_sibling();
    }

    ofstream out("/mnt/win/test.xml");//ofstream 默认时,如果文件存在则会覆盖原来的内容,不存在则会新建
    out << doc;//doc 这样输出时在目标文件中不会有xml 头信息---<?xml version='1.0' encoding='utf-8' >
    out.close();
    return 0;

}


 类似资料: