Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7780

Re: libxml2 with C++ exception

From Philipp Kraus <philipp.kraus@flashpixx.de>
Newsgroups comp.lang.c++
Subject Re: libxml2 with C++ exception
Date 2011-07-13 15:14 +0200
Organization 1&1 Internet AG
Message-ID <ivk5n7$mi2$1@online.de> (permalink)
References <ivjvro$i4t$1@online.de> <c5bf3f98-a5c0-4a44-af6f-2cecb65c703e@n5g2000yqh.googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web