简体   繁体   English

RapidXML解析异常

[英]rapidXML parsing misbehavior

I'm trying to parse a xml file: 我正在尝试解析xml文件:

<?xml version="1.0"?>
<settings>
    <output>test.dat</output>
    <width>5</width>
    <depth>4</depth>
    <height>10</height>
</settings>

main: 主要:

int _tmain(int argc, wchar_t* argv[])
{
    std::string SettingsFile = "settings.xml";
    rapidxml::xml_document<> doc;
    char* settings = FileHandler::readFileInChar(SettingsFile.c_str());

    std::cout << strlen(settings); // Output 1
    doc.parse<0 | rapidxml::parse_no_data_nodes>(settings);
    std::cout << strlen(settings); // Output 2

    ....
}

Output1: 129 输出1:129

Output2: 31 输出2:31

helper functions: 辅助功能:

static char* readFileInChar(const char* p_pccFile) 
{
    char* cpBuffer;
    size_t sSize;

    std::ifstream ifFileToRead;
    ifFileToRead.open(p_pccFile, std::ios::binary);

    if(ifFileToRead.is_open()) {
        sSize = getFileLength(&ifFileToRead);

        cpBuffer = new char[sSize+1];
        ifFileToRead.read(cpBuffer, sSize);
        ifFileToRead.close();
    }

    cpBuffer[sSize] = '\0';

    return cpBuffer;
}

static size_t getFileLength(std::ifstream* file) 
{
    file->seekg(0, std::ios::end);
    size_t length = file->tellg();
    file->seekg(0, std::ios::beg);

    return length;
}

This results in exceptions when I try to access any nodes. 当我尝试访问任何节点时,这会导致异常。 I guess I'm missing something obvious here but so far I don't get it. 我想我这里缺少明显的东西,但到目前为止我还不明白。

If I try something like: 如果我尝试以下操作:

std::cout << doc.first_node("output")->value();

I get the message that there has been an access violation while reading position 0x00000004. 我收到一条消息,指出读取位置0x00000004时发生访问冲突。

The document has no node with the name "output". 该文档没有名称为“输出”的节点。 The document has a node named "settings" which, in turn, has a node named "output". 该文档有一个名为“设置”的节点,而该节点又有一个名为“输出”的节点。 The following code 以下代码

  std::ifstream file("settings.xml");
  std::vector<char> content = std::vector<char>(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>());
  content.push_back('\0');

  rapidxml::xml_document<> doc;
  doc.parse<0 | rapidxml::parse_no_data_nodes>(&content[0]);

  rapidxml::xml_node<> * root = doc.first_node();
  for (rapidxml::xml_node<> * node = root->first_node(); node; node = node->next_sibling())
  {
    std::cout << "value of <" << node->name() << "> is " << node->value() << std::endl;
  }

prints 版画

value of <output> is test.dat
value of <width> is 5
value of <depth> is 4
value of <height> is 10

on my machine. 在我的机器上。

Edit: If you look at "content" in the debugger, you can clearly see where rapidxml inserts '\\0' into it. 编辑:如果您在调试器中查看“内容”,则可以清楚地看到Rapidxml在其中插入“ \\ 0”的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM