1, first download libxml2 (http://download.csdn.net/detail/luoww1/8095273), which includes three folders, respectively: Zlib-1.2.3.win32 and Iconv-1.9.2.win32 and Libxml2-2.7.6.win32
2. Copy the Iconv.dll and Zlib1.dll in the download to the Windows folder under the C drive
3. Create a new include folder in the project, unzip the contents of the Include folder in the libxml2 and iconv into the Include folder, if the project name is ProxyServer, the directory structure
4. VS2010 Project ProxyServer, right click, select "Properties"-->c/c++--> General add in additional inclusion directory
. \include
5, put Iconv.lib and Iconv_a.lib and Libxml2.lib and Libxml2_a.lib in the project root directory, visible above the picture
6, vs2010 Project ProxyServer, right click, select" Properties "--add
Iconv.lib; iconv_a.lib;libxml2.lib;libxml2_a.lib
</PRE>7, engineering libxml2 reading XML file code <pre name= "code" class= "CPP" > #include <iostream> #include < stdio.h> #include <stdlib.h> #include <libxml/parser.h> #include <libxml/tree.h>int main (int argc , char** argv) {xmldocptr doc; Define parse document Pointer xmlnodeptr Curnode; Define the node pointer (you need it in order to move between nodes) Xmlchar *szkey; Temporary string variable char szdocname[] = "Device.xml"; Using Std::string; Using Std::cout; Using Std::endl; doc = Xmlreadfile (szdocname, "UTF-8", xml_parse_recover); Parse File//Check if parsing document is successful, if unsuccessful, Libxml will refer to a registered error and stop. A common error is an inappropriate encoding. XML standard documents can be saved in addition to UTF-8 or UTF-16 other encodings. If the document is this way, Libxml will automatically convert you to UTF-8. More about XML encoding information is included in the XML standard. if (NULL = = doc) {//document open error return-1; } Curnode = Xmldocgetrootelement (DOC); Determine the document root element/* Check to confirm that the current document contains the contents */if (NULL = = Curnode) {//Empty XML file Xmlfreedoc (DOC); Return-2; }/* In this example, we need to verify that the document is of the correct type. "Devices" is the root type of the document used in this example. */if (xmlstrcmp (Curnode->name, BAD_cast "Devices") {//Parse root element failed Xmlfreedoc (DOC); return-3; } Curnode = curnode->xmlchildrennode; Xmlnodeptr propnodeptr = curnode;//xmlnodeptr node list pointer while (Curnode! = NULL) {//Remove the contents of the node if (!XMLSTRCM P (curnode->name, (const XMLCHAR *) "Device"))//Remove the contents of the node Device {szkey = Xmlnodegetcontent (Curnode);//Received Take content cout << szkey << Endl; Xmlfree (Szkey); }//Find node with attribute URL if (xmlhasprop (curnode,bad_cast "url")) {propnodeptr = Curnode; cout<< xmlgetprop (propnodeptr,bad_cast "url") <<endl;//output URL value} curnode = curnode->next; }//Lookup attribute Xmlattrptr attrptr = propnodeptr->properties; A pointer to the Xmlattrptr property list while (attrptr! = NULL) {if (!xmlstrcmp (attrptr->name, bad_cast "url")) { xmlchar* szattr = Xmlgetprop (propnodeptr,bad_cast "url"); cout << szattr << Endl; Locate the properties of the URL Xmlfree (SZATTR); } attrptr = attrptr->next; } xmlfreedoc (doc);//function to close the document pointer and clear the dynamic request memory for all nodes in this document}
8. Contents of Device.xml File
<?xml version= "1.0" encoding= "GB2312"? ><devices><device url= "www.baidu.com" />< Device url= "www.taobao.com"/></devices>
Windows system configures LIBXML2 in VS2010, and reads XML code example