简体   繁体   中英

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.

Firstly getting the data. I did this two ways

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. But it seems to be equivalent and was my original method before I came across rapidxml::file<> . Pretty much the same, just sub in the function to get the file. The returned pointer is passed to 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 . Both cases I get NULL returned from first_node("window") . I get my censored cerr printed, my prompt is indented and xterm fails to work as described below. 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() ). Looks kinda like a black question mark in a white eliptical shape and xterm ceases to function. The next line contains my prompt indented. Key presses do nothing.

Tried removing class before xml_document/node<> , didn't change anything.

Sample from XML File

<!-- 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. You need to get that node first and then get window as a child of it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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