From maxim.maslennikov at gmail.com Tue Feb 17 14:24:58 2015 From: maxim.maslennikov at gmail.com (Maxim Maslennikov) Date: Wed Feb 18 05:09:14 2015 Subject: [studxml-users] Assertion failed: (p.event_ == characters), function characters_, file parser.cxx Message-ID: <6E06CB5C-4179-48FC-A5B1-4B3A6DDD66C8@gmail.com> 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 #include #include #include #include #include #include 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 { 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] << " " << 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 ("orange")); cout << " attr orange " << orange; bool appleis (p.attribute_present("apple")); bool apple; if (appleis) { apple = p.attribute("apple"); cout << " attr apple " << apple; } cout << endl; unsigned int intr (p.element("int")); cout << "\telement int " << intr << endl; double doubler (p.element("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")); 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 From boris at codesynthesis.com Wed Feb 18 08:42:10 2015 From: boris at codesynthesis.com (Boris Kolpackov) Date: Wed Feb 18 08:52:14 2015 Subject: [studxml-users] Assertion failed: (p.event_ == characters), function characters_, file parser.cxx In-Reply-To: <6E06CB5C-4179-48FC-A5B1-4B3A6DDD66C8@gmail.com> References: <6E06CB5C-4179-48FC-A5B1-4B3A6DDD66C8@gmail.com> Message-ID: Hi Maxim, Maxim Maslennikov writes: > I wrote a small example to test the library and get assertion error. The assertion is because of a bug in libstudxml. After fixing it and uncommenting p.content(content::simple) calls in your test (necessary to get character data accumulation), I was able to parse to the end of the file where the unexpected attribute exception is thrown for schemaLocation (because you haven't accessed this attribute and since you are using the high-level attribute map feature, studxml checks that everything is handled). Here is one way to ignore all the unhandled attributes in the root element: p.next_expect(parser::start_element, "test", "root"); p.attribute_map (); // "Tell" studxml we have handled all attributes. I am going to make a bug-fix release shortly. Thanks for the report and the test! Boris From boris at codesynthesis.com Wed Feb 18 09:49:32 2015 From: boris at codesynthesis.com (Boris Kolpackov) Date: Wed Feb 18 09:59:35 2015 Subject: [studxml-users] libstudxml 1.0.1 released Message-ID: Hi, We have released libstudxml 1.0.1. This is a bug-fix only version without any new features. Source code distributions for this release are available from the project's web page: http://www.codesynthesis.com/projects/libstudxml/ SHA1 checksums for this release: 59e40dd960e202648dca6a93b491dbe8823499c7 libstudxml-1.0.1.tar.bz2 1b1a903e1c9e79f02e9d5e87c38d95d98ac17922 libstudxml-1.0.1.tar.gz 8ba2754010bb045dde8cd45e1e59532061284212 libstudxml-1.0.1.zip Enjoy, Boris From maxim.maslennikov at gmail.com Wed Feb 18 13:22:38 2015 From: maxim.maslennikov at gmail.com (Maxim Maslennikov) Date: Thu Feb 19 09:24:08 2015 Subject: [studxml-users] Assertion failed: (p.event_ == characters), function characters_, file parser.cxx In-Reply-To: References: <6E06CB5C-4179-48FC-A5B1-4B3A6DDD66C8@gmail.com> Message-ID: <69D8A776-29FD-4F56-811C-248949FEB078@gmail.com> Hi Boris, Thanks a lot for information. I have just tested the new release it works fine! Best Regards, Maxim Maslennikov Moscow Russia +7 985 274-05-70 > On Feb 18, 2015, at 4:42 PM, Boris Kolpackov wrote: > > Hi Maxim, > > Maxim Maslennikov writes: > >> I wrote a small example to test the library and get assertion error. > > The assertion is because of a bug in libstudxml. After fixing it and > uncommenting p.content(content::simple) calls in your test (necessary > to get character data accumulation), I was able to parse to the end > of the file where the unexpected attribute exception is thrown for > schemaLocation (because you haven't accessed this attribute and since > you are using the high-level attribute map feature, studxml checks > that everything is handled). > > Here is one way to ignore all the unhandled attributes in the root > element: > > p.next_expect(parser::start_element, "test", "root"); > p.attribute_map (); // "Tell" studxml we have handled all attributes. > > I am going to make a bug-fix release shortly. Thanks for the report > and the test! > > Boris From maxim.maslennikov at gmail.com Sat Feb 21 16:54:30 2015 From: maxim.maslennikov at gmail.com (Maxim Maslennikov) Date: Mon Feb 23 08:02:09 2015 Subject: [studxml-users] namespace, prefix & xsi attr Message-ID: Hello! Could you explain how to parse and serialize following root element: I?m able to parse: p.next_expect(parser::start_element, "test", "root", content::complex); string namespace_ = p.namespace_(); string name = p.name(); string prefix = p.prefix(); And serialize: s.start_element( namespace_, name ); s.namespace_decl( namespace, prefix ); but I get only: Is it possible to get and serialize ?xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='test test.xsd?? Best Regards, Maxim Maslennikov Moscow Russia +7 985 274-05-70 From boris at codesynthesis.com Mon Feb 23 08:21:32 2015 From: boris at codesynthesis.com (Boris Kolpackov) Date: Mon Feb 23 08:32:03 2015 Subject: [studxml-users] namespace, prefix & xsi attr In-Reply-To: References: Message-ID: Maxim Maslennikov writes: > Could you explain how to parse and serialize following root element: > > xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' > xsi:schemaLocation='test test.xsd'> > > I?m able to parse: > > p.next_expect(parser::start_element, "test", "root", content::complex); > string namespace_ = p.namespace_(); > string name = p.name(); > string prefix = p.prefix(); >From : const std::string& attribute (const qname_type& qname) const; template T attribute (const qname_type& qname) const; std::string attribute (const qname_type& qname, const std::string& default_value) const; template T attribute (const qname_type& qname, const T& default_value) const; bool attribute_present (const qname_type& qname) const; So: string schema_location = p.attribute (qname ("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation")); > And serialize: > s.start_element( namespace_, name ); > s.namespace_decl( namespace, prefix ); >From : void attribute (const qname_type& qname, const std::string& value); template void attribute (const qname_type& qname, const T& value); void attribute (const std::string& ns, const std::string& name, const std::string& value); template void attribute (const std::string& ns, const std::string& name, const T& value); And: // Namespaces declaration. If prefix is empty, then the default // namespace is declared. If both prefix and namespace are empty, // then the default namespace declaration is cleared (xmlns=""). // void namespace_decl (const std::string& ns, const std::string& prefix); So: s.namespace_decl ("http://www.w3.org/2001/XMLSchema-instance", "xsi"); s.attribute ("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", "test test.xsd"); Moral of the story: don't be afraid to look into the API headers, it's all in there. Boris From maxim.maslennikov at gmail.com Mon Feb 23 16:22:36 2015 From: maxim.maslennikov at gmail.com (Maxim Maslennikov) Date: Tue Feb 24 02:13:44 2015 Subject: [studxml-users] namespace, prefix & xsi attr In-Reply-To: References: Message-ID: Boris, Thank you a lot for the explanation and advice! Best Regards, Maxim Maslennikov Moscow Russia +7 985 274-05-70 > On Feb 23, 2015, at 4:21 PM, Boris Kolpackov wrote: > > Maxim Maslennikov writes: > >> Could you explain how to parse and serialize following root element: >> >> > xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >> xsi:schemaLocation='test test.xsd'> >> >> I?m able to parse: >> >> p.next_expect(parser::start_element, "test", "root", content::complex); >> string namespace_ = p.namespace_(); >> string name = p.name(); >> string prefix = p.prefix(); > > From : > > const std::string& > attribute (const qname_type& qname) const; > > template > T > attribute (const qname_type& qname) const; > > std::string > attribute (const qname_type& qname, > const std::string& default_value) const; > > template > T > attribute (const qname_type& qname, const T& default_value) const; > > bool > attribute_present (const qname_type& qname) const; > > So: > > string schema_location = > p.attribute (qname ("http://www.w3.org/2001/XMLSchema-instance", > "schemaLocation")); > >> And serialize: >> s.start_element( namespace_, name ); >> s.namespace_decl( namespace, prefix ); > > From : > > void > attribute (const qname_type& qname, const std::string& value); > > template > void > attribute (const qname_type& qname, const T& value); > > void > attribute (const std::string& ns, > const std::string& name, > const std::string& value); > > template > void > attribute (const std::string& ns, > const std::string& name, > const T& value); > > And: > > // Namespaces declaration. If prefix is empty, then the default > // namespace is declared. If both prefix and namespace are empty, > // then the default namespace declaration is cleared (xmlns=""). > // > void > namespace_decl (const std::string& ns, const std::string& prefix); > > So: > > s.namespace_decl ("http://www.w3.org/2001/XMLSchema-instance", "xsi"); > s.attribute ("http://www.w3.org/2001/XMLSchema-instance", > "schemaLocation", > "test test.xsd"); > > Moral of the story: don't be afraid to look into the API headers, it's > all in there. > > Boris