简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM