From: Balazs T. <de...@ba...> - 2008-01-29 11:01:49
|
Hi! I'm using libxml++ to parse an xml file like this: . . xmlpp::DomParser parser; parser.set_validate(true); parser.parse_file("example.xml"); . . The example.xml file looks like this: <?xml version="1.0"?> <!-- Example from XMLSchema recommendation 0 - primer at http://www.w3.org/TR/xmlschema-0/ --> <purchaseOrder orderDate="1999-10-20"> <shipTo country="US"> <nme>Alice Smith</name> . . . and as you can see it contains an error at "<nme>Alice Smith</name>", that's fine. Now.. when i'm running the program, i got this on the console: example.xml:5: parser error : Opening and ending tag mismatch: nme line 5 and name <nme>Alice Smith</name> ^ Which is - of course - OK, because it really contains errors, but i cannot control this error message with libxml++. I mean i got an exception whit this message: "Validity error:Validation failed: no DTD found !" but I can't get anything else. So what I want is that "opening and ending tag mismatch..." message do not appear on the console, instead I should control what should appear and what is not. Is there a simple way to do that in libxmlpp? (I found a function in libxml called: xmlSetStructuredErrorFunc. When i use this function to set a callback, then it does exactly what i want. It does not send anything to the console instead it calls that callback function with the error message, and i can decide what i want to do that message.) |
From:
<glu...@gm...> - 2008-01-30 07:05:30
|
Hi Balazs! First: You don't have DTD in your xml. libxml++ can read it from your xml, or from an another file: <?xml version="1.0"?> <!DOCTYPE settings SYSTEM "myxml.dtd"> You can't validate your xml with the DomParser without a correct DTD. Second: You need catch the exceptions. Something like this: try { conf->load_general_settings("general.xml"); } catch (xmlpp::internal_error& ex) { Gtk::MessageDialog dlg(_("Internal error in the xml parser!"),false, Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK); dlg.set_secondary_text(ex.what()); dlg.run(); return -1; } catch (xmlpp::validity_error& ex) { ... } catch (xmlpp::exception& ex) { Gtk::MessageDialog dlg(_("Error in the xml parser!"),false, Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK); dlg.set_secondary_text(ex.what()); dlg.run(); return -1; } In this program I should not work with the bad xml, but sometimes it possible. In this case you need only a try-catch block in your code at the read, and it will work fine. But don't forget send a message to the user. Much easier to correct the bug in the xml, if you know, it is there. If you have problem with the validation, you can switch it of, and force the reading. Something like this: void read_from_xml(const char* file, bool validation=true) { xmlpp::DomParser parser; parser.set_validate(validation); parser.parse_file(file); ... } try { read_from_xml("myxml.xml"); } catch (xmlpp::validity_error& ex) { log.add_warning("myxml validation failed!"); read_from_xml("myxml.xml",false); } Attila |
From: dexter <de...@ba...> - 2008-01-30 10:48:09
|
Hi! First of all, I think you slightly misunderstood what i wrote. 1. I'm aware of that I don't have DTD in my xml. I have a very good reason for that. Namely, I don't need it. I'll do the validation with a schema and not with DTD. (I also know that libxml++ doesn't contain a wrapper to do that, but i've already applied Emilien KIA's patch, so now it is capable to do validation with schemas) 2. The problem is not with the validation (It does not even get there). The problem is that the xml is _not_ well formed. And what I would like is to get the error somehow in my program, but instead it just writes error messages to the console. Of course I use exceptions as you wrote, and I catch everything at the end [catch (...)], but this error cannot be catched because - as i noticed - it doesn't raise an exception. With the method I mentioned in my previous mail, I'm able to catch those error, but I thougth - maybe - there's a simplier way to do that. I hope it's more clear now. -- Balazs > ----- Original Message ----- > From: Glück Attila Béla <glu...@gm...> > To: lib...@li... > Sent: Wed, Jan 30, 2008 8:04:56 AM +0100 > Subject: [libxml++] parser problem > > Hi Balazs! > > First: You don't have DTD in your xml. libxml++ can read it from your > xml, or from an another file: > <?xml version="1.0"?> > <!DOCTYPE settings SYSTEM "myxml.dtd"> > You can't validate your xml with the DomParser without a correct DTD. > > Second: You need catch the exceptions. > Something like this: > try { > conf->load_general_settings("general.xml"); > } catch (xmlpp::internal_error& ex) { > Gtk::MessageDialog dlg(_("Internal error in the xml > parser!"),false, Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK); > dlg.set_secondary_text(ex.what()); dlg.run(); > return -1; > } catch (xmlpp::validity_error& ex) { > ... > } catch (xmlpp::exception& ex) { > Gtk::MessageDialog dlg(_("Error in the xml parser!"),false, > Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK); > dlg.set_secondary_text(ex.what()); > dlg.run(); > return -1; > } > > In this program I should not work with the bad xml, but sometimes > it possible. In this case you need only a try-catch block in > your code at the read, and it will work fine. But don't forget send a > message to the user. Much easier to correct the bug in the xml, if > you know, it is there. > If you have problem with the validation, you can switch it of, > and force the reading. > Something like this: > > void read_from_xml(const char* file, bool validation=true) > { > xmlpp::DomParser parser; > parser.set_validate(validation); > parser.parse_file(file); > .. > } > > try { > read_from_xml("myxml.xml"); > } catch (xmlpp::validity_error& ex) { > log.add_warning("myxml validation failed!"); > read_from_xml("myxml.xml",false); > } > > Attila > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > _______________________________________________ > Libxmlplusplus-general mailing list > Lib...@li... > https://lists.sourceforge.net/lists/listinfo/libxmlplusplus-general > |