简体   繁体   English

RapidXML:Segfault; first_node()返回NULL

[英]RapidXML: Segfault; first_node() returns NULL

I've tried a few things and am arriving at the same place. 我尝试了一些事情,然后到达了同一地方。 Basically after calling first_node() I get NULL or nonsensical binary which messes up xterm which then I need to close and reopen. 基本上,在调用first_node()我得到NULL或无意义的二进制文件,弄乱了xterm,然后我需要关闭并重新打开。

Firstly getting the data. 首先获取数据。 I did this two ways 我做了这两种方式

1. RapidXML file<> 1. RapidXML file<>

#include <xml/rapidxml.hpp> //--XML Parser for configuration files
#include <xml/rapidxml_utils.hpp>

//....

file<> xml_l(name_layout.c_str());
class xml_document<> doc_l;
class xml_node<> * node;

//....

doc_l.parse<parse_full>(xml_l.data());

//....

node = doc_l.first_node("window");

if(!node) cerr << "F F F F" << endl;

2. Static Variable Probably not the best way. 2.静态变量可能不是最好的方法。 But it seems to be equivalent and was my original method before I came across rapidxml::file<> . 但这似乎是等效的,并且是我遇到rapidxml::file<>之前的原始方法。 Pretty much the same, just sub in the function to get the file. 几乎相同,只是在函数中添加文件即可。 The returned pointer is passed to xml_document::parse() . 返回的指针将传递给xml_document::parse()

char * file_get_contents (const string &file) {
    ifstream ifile;
    static vector< vector<char> > xml;
    vector<char> data;


    ifile.exceptions( ifstream::failbit | ifstream::badbit );

    try {
        ifile.open(file.c_str(),ios::in | ios::binary);
        ifile.seekg(0, ios::end);

        data.resize(((int)ifile.tellg())+1);

        ifile.seekg(0, ios::beg);
        ifile.read(&data[0], data.size()-1);
        ifile.close();

        data.back() = '\0';
        xml.push_back(data);

        return const_cast<char *>(&((xml.back())[0]));
    } catch (ifstream::failure e) {
        cerr << "Could not open file: " << file.c_str() << endl;
    }
}

I can take the pointer returned by either method and display the whole file with cout . 我可以使用任一方法返回的指针,并使用cout显示整个文件。 Both cases I get NULL returned from first_node("window") . 两种情况下,我都从first_node("window")返回NULL I get my censored cerr printed, my prompt is indented and xterm fails to work as described below. 我打印了经过审查的cerr ,缩进了提示,并且xterm不能按如下所述工作。 If I call it with no arguments I get an element node. 如果我不带参数调用它,则会得到一个元素节点。 If I try to display the name or value I have one character I can see (from name() never makes it to value() ). 如果我尝试显示名称或值,我可以看到一个字符(从name()value()不会)。 Looks kinda like a black question mark in a white eliptical shape and xterm ceases to function. 看起来有点像白色椭圆形的黑色问号,并且xterm不再起作用。 The next line contains my prompt indented. 下一行包含我的缩进提示。 Key presses do nothing. 按键不起作用。

Tried removing class before xml_document/node<> , didn't change anything. 尝试删除xml_document/node<>之前的classxml_document/node<>任何更改。

Sample from XML File XML文件样本

<!-- language: lang-xml -->
<layout>
<window id="app_header">
    <head>
        <title value="Script Manager" />
        <color
            fgcolor="yellow"
            bgcolor="blue"
            intensity="high"
        />
    </head>
    <height min="1" max="1" value="1" />
    <width value="*" />
    <color
        fgcolor="default"
        bgcolor="default"
    />
</window>
<!--Few more window sections described. Same format. Different height values and colors -->
</layout>

window is not the root node of your xml file, layout is. window不是xml文件的根节点,而layout是。 You need to get that node first and then get window as a child of it. 您需要先获取该节点,然后再将window作为其子节点。

xml_node<> rootNode = doc_l.first_node("layout", 6);
xml_node<> windowNode = rootNode.first_node("window", 6);

Rapidxml parses the xml into a hierarchical structure so you need to traverse it as a tree. Rapidxml将xml解析为分层结构,因此您需要将其作为树遍历。

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

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