windows 系統在vs2010 中配置libxml2,及讀取xml代碼例子

來源:互聯網
上載者:User

標籤:windows   vs2010   libxml2   zlib   iconv   

1、先下載libxml2 ( http://download.csdn.net/detail/luoww1/8095273),裡麵包括了三個檔案夾分別:zlib-1.2.3.win32 和iconv-1.9.2.win32 和libxml2-2.7.6.win32

2、將下載內容中的iconv.dll 和zlib1.dll 拷貝到C盤下的Windows檔案夾中

3、在項目中建立include 檔案夾,解壓的到的libxml2和iconv中的include檔案夾中的內容拷入到該include檔案夾中,若項目名稱為proxyServer,目錄結構


4、VS2010項目proxyServer,右擊,選擇“屬性”-->C/C++-->常規 在附加元件封裝含目錄中添加

.\include

5、將iconv.lib 和iconv_a.lib和libxml2.lib和libxml2_a.lib放在項目根目錄中,可見上面的圖片

6、VS2010項目proxyServer,右擊,選擇“屬性”-->連結器-->輸入中添加

iconv.lib ;iconv_a.lib;libxml2.lib;libxml2_a.lib

</pre>7、工程中用libxml2讀取xml檔案的代碼<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;           //定義解析文檔指標    xmlNodePtr curNode;      //定義結點指標(你需要它為了在各個結點間移動)    xmlChar *szKey;          //臨時字串變數    char szDocName[] = "Device.xml";    using std::string;    using std::cout;    using std::endl;        doc = xmlReadFile(szDocName,"UTF-8",XML_PARSE_RECOVER); //解析檔案    //檢查解析文檔是否成功,如果不成功,libxml將指一個註冊的錯誤並停止。    //一個常見錯誤是不適當的編碼。XML標準文檔除了用UTF-8或UTF-16外還可用其它編碼儲存。    //如果文檔是這樣,libxml將自動地為你轉換到UTF-8。更多關於XML編碼資訊包含在XML標準中.    if (NULL == doc)    {       //文檔開啟錯誤       return -1;    }    curNode = xmlDocGetRootElement(doc); //確定文檔根項目    /*檢查確認當前文檔中包含內容*/    if (NULL == curNode)    {       //空得xml檔案       xmlFreeDoc(doc);       return -2;    }    /*在這個例子中,我們需要確認文檔是正確的類型。“Devices”是在這個樣本中使用文檔的根類型。*/    if (xmlStrcmp(curNode->name, BAD_CAST "Devices"))    {       //分析根項目失敗       xmlFreeDoc(doc);       return -3;    }    curNode = curNode->xmlChildrenNode;    xmlNodePtr propNodePtr = curNode;//xmlNodePtr 節點列表的指標    while(curNode != NULL)    {       //取出節點中的內容       if ((!xmlStrcmp(curNode->name, (const xmlChar *)"Device")))  //取出節點Device的內容       {           szKey = xmlNodeGetContent(curNode);//擷取內容           cout << szKey << endl;            xmlFree(szKey);       }       //尋找帶有屬性url的節點       if (xmlHasProp(curNode,BAD_CAST "url"))       {           propNodePtr = curNode;   //cout<< xmlGetProp(propNodePtr,BAD_CAST "url") <<endl;//輸出URL的值       }       curNode = curNode->next;    }  //尋找屬性    xmlAttrPtr attrPtr = propNodePtr->properties; //xmlAttrPtr 屬性列表的指標    while (attrPtr != NULL)    {       if (!xmlStrcmp(attrPtr->name, BAD_CAST "url"))       {           xmlChar* szAttr = xmlGetProp(propNodePtr,BAD_CAST "url");           cout << szAttr << endl; //找到url的屬性           xmlFree(szAttr);       }       attrPtr = attrPtr->next;    }    xmlFreeDoc(doc);//函數關閉文檔指標,並清除本文檔中所有節點動態申請的記憶體}
8、Device.xml 檔案的內容

<?xml version="1.0" encoding="GB2312"?><Devices><Device  url="www.baidu.com"  /><Device url="www.taobao.com"/></Devices>



windows 系統在vs2010 中配置libxml2,及讀取xml代碼例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.