[studxml-users] Assertion failed: (p.event_ == characters),
function characters_, file parser.cxx
Maxim Maslennikov
maxim.maslennikov at gmail.com
Tue Feb 17 14:24:58 EST 2015
Hello!
I wrote a small example to test the library and get assertion error.
The program output:
record attr orange 0 attr apple 1
element int 42
element double 42345.4
element name name123_45
element choice 1 choice
element enum 0
...
record attr orange 24 attr apple 1
element int 42
element double 42345.4
element name name123_45
element choice 1 choice
Assertion failed: (p.event_ == characters), function characters_, file parser.cxx, line 906.
My program is below. I used a input file from example/performance - test-50k.xml
Could you explain where I make mistake?
#include <iostream>
#include <string>
#include <fstream>
#include <regex>
#include <xml/parser>
#include <xml/serializer>
#include <xml/value-traits>
using namespace std;
using namespace xml;
enum enum_type {romance, fiction, horror, history, philosophy};
// Specialization of xml::value_traits for object_type. This mechanism is
// used to implements XML-specific value type parsing and serialization.
// By default the parser and serializer fall back onto iostream insertion
// and extraction operators.
//
// This code also shows how we can reuse the parsing exception to implement
// our own diagnostics.
//
namespace xml
{
template <>
struct value_traits<enum_type>
{
static enum_type
parse (string s, const parser& p)
{
if (s == "romance")
return romance;
else if (s == "fiction")
return fiction;
else if (s == "horror")
return horror;
else if (s == "history")
return history;
else if (s == "philosophy")
return philosophy;
else
throw parsing (p, "invalid object type '" + s + "'");
}
static string
serialize (enum_type e, const serializer&)
{
if (e == romance)
return "romance";
else if (e == fiction)
return "fiction";
else if (e == horror)
return "horror";
else if (e == history)
return "history";
else
return "philosophy";
}
};
}
int main(int argc, const char * argv[]) {
// insert code here...
if (argc != 2)
{
cerr << "usage: " << argv[0] << " <xml-file>" << endl;
return 1;
}
try
{
// Parse the input document and compute the average object position.
//
ifstream ifs (argv[1]);
parser p (ifs, argv[1]);
unsigned int count (0);
p.next_expect(parser::start_element, "test", "root");
do {
p.next_expect (parser::start_element, "record", content::complex);
cout << "record";
unsigned long orange (p.attribute<unsigned long> ("orange"));
cout << " attr orange " << orange;
bool appleis (p.attribute_present("apple"));
bool apple;
if (appleis) {
apple = p.attribute<bool>("apple");
cout << " attr apple " << apple;
}
cout << endl;
unsigned int intr (p.element<unsigned int>("int"));
cout << "\telement int " << intr << endl;
double doubler (p.element<double>("double"));
cout << "\telement double " << doubler << endl;
string name (p.element("name"));
cout << "\telement name " << name << endl;
bool stringis;
string stringr;
string choice;
p.next_expect(parser::start_element);
if ( p.name() == "string" )
{
stringis = true;
//p.content(content::simple);
p.next_expect(parser::characters);
stringr = p.value();
p.next_expect(parser::end_element);
p.next_expect(parser::start_element);
cout << "\telement string " << stringr << endl;
}
if (std::regex_match (p.name(), std::regex("choice[1234]") ))
{
//p.content (content::simple);
p.next_expect(parser::characters);
choice = p.value();
p.next_expect(parser::end_element);
cout << "\telement choice " << choice << endl;
}
else
throw parsing(p, "invalid object type '" + p.name() + "'");
enum_type enumr (p.element<enum_type>("enum"));
cout << "\telement enum " << enumr << endl;
p.next_expect (parser::end_element); // record
++count;
//cout << '\r' << count;
} while (p.peek () == parser::start_element);
p.next_expect(parser::end_element); // t:root
cout << "Count: " << count << endl;
}
// This handler will handle both parsing (xml::parsing) and serialization
// (xml::serialization) exceptions.
//
catch (const xml::exception& e)
{
cerr << e.what () << endl;
return 1;
}
}
Best Regards,
Maxim Maslennikov
Moscow Russia
+7 985 274-05-70
More information about the studxml-users
mailing list