JohnG
JohnG

Reputation: 837

how to get off with the libxml2 error messages

I want to use the libxml2 lib to parse my xml files. Now, when I have some bad xml file the lib itself is printing large error messages.

below is some sample code

reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
   ret = xmlTextReaderRead(reader);
   while (ret == 1) {
       printf("_________________________________\n");
       processNode(reader);
       ret = xmlTextReaderRead(reader);
       printf("_________________________________\n");
   }
   xmlFreeTextReader(reader);
   if (ret != 0) {
       fprintf(stderr, "%s : failed to parse\n", filename);
   }
}

In above example, if I have bad xml file, I get error like this

my.xml:4: parser error : attributes construct error
 include type="text"this is text. this might be excluded in the next occurrence 

my.xml:4: parser error : Couldn't find end of Start Tag include
 include type="text"this is text. this might be excluded in the next occurrence 

my.xml : failed to parse

Instead, I just want to return some error no. and get off with this ugly lib messages.

what do I do ?

Upvotes: 2

Views: 2396

Answers (1)

JeremyP
JeremyP

Reputation: 86691

The last parameter to xmlReaderForFile(filename, NULL, 0); is a set of option flags. Reading the documentation for these flags, I see there are two options you might want to set: XML_PARSE_NOERROR and XML_PARSE_NOWARNING. Note that I haven't tried any of this, I just Googled libxml2 and xmlReaderForFile.

You will need to or the flags together like this:

reader = xmlReaderForFile(filename, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);

Upvotes: 4

Related Questions