xmlParseFile vs xmlReadFile (libxml2)

2019-05-20 16:43发布

问题:

I'm writing some C code using the libxml2 library to read an XML file. There seem to be two different functions for this purpose, xmlParseFile and xmlReadFile, and and I'm not sure of the difference between them (besides the fact that xmlReadFile() takes some additional parameters).

The examples on the libxml2 website sometimes use xmlParseFile and some use xmlReadFile.

So when should you use xmlParseFile and when should you use xmlReadFile? I haven't been able to find anything that explains this.

回答1:

xmlReadFile() is a bit more powerful as it is able to take an URL instead of a local file path, and allows to specify more options (http://xmlsoft.org/html/libxml-parser.html#xmlParserOption), so I tend to use it instead of xmlParseFile(). That said, if you are parsing a local XML file and not using the parser options, you will be fine with xmlParseFile().



回答2:

xmlReadFile() is more powerful and latest version for parsing the XML. I am also using it in place of xmlParseFile().



回答3:

I have xml arriving in character buffer 'msg' on TCP pipe so I use libxml2 call xmlReadDoc() instead as following with options XML_PARSE_NOBLANKS and XML_PARSE_OLD10

xmlDocPtr parsed_xml_dom;
parsed_xml_dom = xmlReadDoc((xmlChar *)(msg), NULL, NULL, XML_PARSE_NOBLANKS| XML_PARSE_OLD10);


标签: c libxml2