libxml2 with C++ exception

P

Philipp Kraus

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
 
M

Michael DOUBEZ

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)
 
P

Philipp Kraus

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
*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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top