Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Get output from libxml2

Hi all,

I'm trying to use libxml2 in C. I want to validate an XML string against a DTD file.
The following code works fine:

doc = xmlParseMemory((char *)xmlString, strlen(xmlString));
dtd = xmlParseDTD(NULL, (const xmlChar*)dtdPath);
if (dtd != NULL)
{
xmlValidCtxt vctx;
memset(&vctx, 0, sizeof(vctx));

result = xmlValidateDtd(&vctx, doc, dtd);
}

It prints an error such as:
element Session: validity error : Element Session content does not follow the DTD
element SessionDescriptor: validity error : Element SessionDescriptor content does not follow the DTD

but... xmlValidateDtd returns an integer. How do I obtain the error printed on screen so I can use it in a GUI?

Cheers,
Mark
[796 byte] By [qalmad] at [2007-11-11 10:03:47]
# 1 Re: Get output from libxml2
The following function retrieves the last error and returns it as a std::string (excuse use of C++ std lib):

std::string getLastError()
{
xmlErrorPtr err = xmlGetLastError();

if (!err)
return "";

std::stringstream ss;
ss << "Error at line ";
ss << err->line;
if (err->int2)
{
ss << ", column ";
ss << err->int2;
}
ss << ": ";
ss << err->message;
return ss.str();
}
geralds at 2007-11-11 23:28:23 >
# 2 Re: Get output from libxml2
Thanks geralds!!

I'll take a while to digest that... my c is not very 'sharp' ;)

Have a great 2007!
qalmad at 2007-11-11 23:29:29 >