Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7777 > unrolled thread
| Started by | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| First post | 2011-07-13 13:34 +0200 |
| Last post | 2011-07-13 15:14 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.c++
libxml2 with C++ exception Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-07-13 13:34 +0200
Re: libxml2 with C++ exception Michael DOUBEZ <michael.doubez@free.fr> - 2011-07-13 05:25 -0700
Re: libxml2 with C++ exception Philipp Kraus <philipp.kraus@flashpixx.de> - 2011-07-13 15:14 +0200
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2011-07-13 13:34 +0200 |
| Subject | libxml2 with C++ exception |
| Message-ID | <ivjvro$i4t$1@online.de> |
Hello,
I' using for XML parsing the libxml2 library and I use the native C
interface (not the C++ bindings). I parse a std::string with this code
const char* l_xmlcontent = p_xml.c_str();
xmlDocPtr l_xml = xmlParseMemory( l_xmlcontent,
strlen(l_xmlcontent) );
p_xml is a std::string. In some cases the XML data within the string
can't be valid and I get the correct message:
Entity: line 1: parser error : Start tag expected, '<' not found
The message is shown on the command line on each time, but I would like
to catch the error and would like to throw an exception. A try-catch
statement around the xmlParseMemory does not work. I've found some
notes that I can use a callback function, which is run on an error, but
I don't find any information for the implementation.
I hope my question isn't off-topic and anyonw can help me to create on
a parser error a correct exception
Thanks
Phil
[toc] | [next] | [standalone]
| From | Michael DOUBEZ <michael.doubez@free.fr> |
|---|---|
| Date | 2011-07-13 05:25 -0700 |
| Message-ID | <c5bf3f98-a5c0-4a44-af6f-2cecb65c703e@n5g2000yqh.googlegroups.com> |
| In reply to | #7777 |
On Jul 13, 1:34 pm, Philipp Kraus <philipp.kr...@flashpixx.de> wrote: > I' using for XML parsing the libxml2 library and I use the native C > interface (not the C++ bindings). I parse a std::string with this code > const char* l_xmlcontent = p_xml.c_str(); > xmlDocPtr l_xml = xmlParseMemory( l_xmlcontent, > strlen(l_xmlcontent) ); > > p_xml is a std::string. In some cases the XML data within the string > can't be valid and I get the correct message: > Entity: line 1: parser error : Start tag expected, '<' not found > > The message is shown on the command line on each time, but I would like > to catch the error and would like to throw an exception. Catching the error is easy: libxml2 let you define the error handlers: see the xml*ErrorFunc() functions in the API. > A try-catch > statement around the xmlParseMemory does not work. I've found some > notes that I can use a callback function, which is run on an error, but > I don't find any information for the implementation. *BUT* propagating an exception through C code is a bad idea. The libxml2 doesn't expect to loose flow for control and leaks are likely to occur. The proper solution is to build a C++ wrapper that stores errors and throw them upon error return code (you can use xmlCtxtGetLastError/xmlGetLastError) -- Michael
[toc] | [prev] | [next] | [standalone]
| From | Philipp Kraus <philipp.kraus@flashpixx.de> |
|---|---|
| Date | 2011-07-13 15:14 +0200 |
| Message-ID | <ivk5n7$mi2$1@online.de> |
| In reply to | #7779 |
On 2011-07-13 14:25:17 +0200, Michael DOUBEZ said: > On Jul 13, 1:34 pm, Philipp Kraus <philipp.kr...@flashpixx.de> wrote: >> I' using for XML parsing the libxml2 library and I use the native C >> interface (not the C++ bindings). I parse a std::string with this code >> const char* l_xmlcontent = p_xml.c_str(); >> xmlDocPtr l_xml = xmlParseMemory( l_xmlcontent, >> strlen(l_xmlcontent) ); >> >> p_xml is a std::string. In some cases the XML data within the string >> can't be valid and I get the correct message: >> Entity: line 1: parser error : Start tag expected, '<' not found >> >> The message is shown on the command line on each time, but I would like >> to catch the error and would like to throw an exception. > > Catching the error is easy: libxml2 let you define the error handlers: > see the xml*ErrorFunc() functions in the API. oh thanks a lot :-P > >> A try-catch >> statement around the xmlParseMemory does not work. I've found some >> notes that I can use a callback function, which is run on an error, but >> I don't find any information for the implementation. > > *BUT* propagating an exception through C code is a bad idea. The > libxml2 doesn't expect to loose flow for control and leaks are likely > to occur. The proper solution is to build a C++ wrapper that stores > errors and throw them upon error return code (you can use > xmlCtxtGetLastError/xmlGetLastError) I do this for a special XML structure. Parsing XML, checking errors, clearing all data and throwing an error if needed.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web